I have a general view, where I "insert" a specific view into the contained ContentControl
(I created this function using this help -> help 1 - help 2 ).
The main source of my views are the following:
MyGenericView.xaml
<UserControl x:Class="MyNS.MyGenericView" ... > <UserControl.Resources> <vml:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> </UserControl.Resources> <Grid DataContext="{Binding MyGenericViewModel, Source={StaticResource Locator}}"> <ContentControl Content="{Binding MyObject}" /> </Grid> </UserControl>
CustomerView.xaml
<UserControl x:Class="AnotherNS.CustomerView" ... > <Grid> <StackPanel Orientation="Vertical"> <Label Content="Description" /> <TextBox Text="{Binding description}" /> </StackPanel> </Grid> </UserControl>
Crud.xaml
: a resource dictionary that I use to “solve” what shows the right kind, depending on the DataType
of the MyObject
provided by the general view.
<ResourceDictionary ... > <DataTemplate DataType="{x:Type mo:Customer}"> <vw:CustomerView /> </DataTemplate> <DataTemplate DataType="{x:Type mo:Product}"> <vw:ProductView /> </DataTemplate> ... </ResourceDictionary>
It is working fine. I can control MyObject
through a "specific" view (client, product, etc.).
Well. What is my problem:
All specific views have their own ViewModels and, of course, they manage the data of the respective views. But I don’t know (on the viewmodel) which object (MyObject) I work with, because the Generic View provides it with a specific view, not a viewmodel.
Is there a way to let ViewModels of certain views recognize the object that “commands” the view?
source share