Reputation: 21855
I've developed an application with WPF and MVVM. In it I have a Window with a DataGrid. It's ViewModel contains some properties for the window and one property for the DataGrid (an ObservableCollection<DataGridItemViewModel>
).
In the window xaml I set the design DataContext in this way:
<Window x:Class="XXX"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance TheTypeOfTheWindowViewModelHere}">
Then I want to set the design DataContext onto the DataGrid this way:
<DataGrid ItemsSource="{Binding Path=PropertyOfTheDataGrid}" d:DataContext="{d:DesignInstance DataGridItemViewModel}" >
But then I get a warning telling me that cannot find PropertyOfTheDataGrid inside DataGridItemViewModel.
I thought I was setting only the DataContext of the ItemsSource but not I'm not sure if I'm doing it wrong or if its an issue of some kind.
Thanks in advance.
Upvotes: 2
Views: 4538
Reputation: 34349
I'm not quite sure what you were expecting? From your naming standard, you have a DataGridItemViewModel
which suggests you were expecting to apply a view model context to each data grid item?
Normally, you would apply one view model to your entire view, and then have a property on that view model which is for example an ObservableCollection
, which is your collection of items for your grid. Then you would set the ItemsSource
of your DataGrid
to bind to that collection property.
ItemsSource="{Binding MyItems}"
You wouldn't normally need to set the data context of the grid directly, it would use the data context of the view (a Window
in this case).
Upvotes: 4