【发布时间】:2012-02-16 02:16:38
【问题描述】:
我有一个通用视图,我将一些特定视图“注入”到包含的 ContentControl 中(我在这些帮助下创建了该功能 -> help 1 - help 2)。
我的观点的基本来源是:
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:我用来“解决”显示正确视图的资源字典,具体取决于通用视图提供的MyObject 对象的DataType。
<ResourceDictionary ... >
<DataTemplate DataType="{x:Type mo:Customer}">
<vw:CustomerView />
</DataTemplate>
<DataTemplate DataType="{x:Type mo:Product}">
<vw:ProductView />
</DataTemplate>
...
</ResourceDictionary>
一切正常。我可以通过“特定”视图(客户、产品等)管理MyObject。
嗯。那是我的问题:
所有特定视图都有自己的 ViewModel,当然,它们管理各自视图的数据。但我不知道(在视图模型上)我正在使用的对象(MyObject)是什么,因为通用视图将它提供给特定视图,而不是视图模型。
有没有办法让特定 View 的 ViewModel 知道正在“控制”该视图的对象?
【问题讨论】: