I'm a novice trying to conform to MVVM pattern, and understand that the main purpose of the ViewModel is to act as an intermediary between the Model and the View.
My issue is, I get binding errors when I target properties that I have exposed to my ViewModel.
I have a DataAccess method in my Model (LoadInvoices). In my ViewModel I call it like so:
This works as it should and I use it to populate a DataGrid. I use the SelectedItem of the grid to send SelectedItem to a child window.
The child window DataContext is the corresponding ViewModel, but when I try to bind a control in my child view I get the binding error:
CompanyId property not found on object of type InvoicingViewModel.
The program compiles, runs and returns correct data but with binding failures which is niggling me.
If I use the Model DataContext, no errors but I feel that's not what I should be doing. The obvious solution is to create the model properties in the viewModel, which would question the need for a model?
Any help appreciated.
My issue is, I get binding errors when I target properties that I have exposed to my ViewModel.
I have a DataAccess method in my Model (LoadInvoices). In my ViewModel I call it like so:
C#:
public InvoicingViewModel()
{
LoadinvoiceData();
}
public void LoadinvoiceData()
{
_invoicingModel = new InvoicingModel();
List<InvoicingModel> invoiceList = _invoicingModel.LoadInvoices();
GetInvoiceData = new ObservableCollection<InvoicingModel>(invoiceList);
}
public ObservableCollection<InvoicingModel>? GetInvoiceData
{
get { return _invoicing; }
set
{
_invoicing = value;
OnPropertyChanged(nameof(GetInvoiceData));
}
}
The child window DataContext is the corresponding ViewModel, but when I try to bind a control in my child view I get the binding error:
CompanyId property not found on object of type InvoicingViewModel.
C#:
<Label Content="Company Id:"
FontSize="14"
FontWeight="Bold"
HorizontalAlignment="Right" />
<TextBlock Text="{Binding Path=CompanyId}" />
The program compiles, runs and returns correct data but with binding failures which is niggling me.
If I use the Model DataContext, no errors but I feel that's not what I should be doing. The obvious solution is to create the model properties in the viewModel, which would question the need for a model?
Any help appreciated.