Reputation: 31
that's perhaps an absolute newbye question.
I try to bind a simple Viewmodel Person.allinstances with Firstname: string, Lastname: string to WPF DataGrid.
ViewModel:
XAML:
<Window.Resources>
<ecoVM:ViewModelContent x:Key="VM1" ViewModelName="AllPersons" EcoSpaceType="{x:Type ecospace:RelativesEcoSpace}" ></ecoVM:ViewModelContent>
</Window.Resources>
<Grid DataContext="{StaticResource VM1}">
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding ?????}" />
</Grid>
Code Behind:
(Resources["VM1"] as ViewModelContent).SetEcoSpace(_es); (Resources["VM1"] as ViewModelContent).RootObject = ???;
How should I do it properly?
Upvotes: 1
Views: 81
Reputation: 2435
When you create the ViewModel - check the CodeGen-checkbox.
Then CodeGen - and you have a strongly typed VM at your disposal
Use like this:
xmlns:MyVm="clr-namespace:MDrivenMinimalWecpof3.ViewModelCodeGen_FirstView"
...
<MyVm:FirstView x:Key="MyVm" ></MyVm:FirstView>
...
<Grid Grid.Row="2" DataContext="{StaticResource MyVm}">
<DataGrid ItemsSource="{Binding Path=AllSomeClass }" AutoGenerateColumns="True"></DataGrid>
</Grid>
In code behind you set the EcoSpace and possibly root:
(this.Resources["MyVm"] as ViewModelCodeGen_FirstView.FirstView).SetEcoSpace(_es);
Upvotes: 0