【发布时间】:2015-01-24 08:31:35
【问题描述】:
我有一个关于使用 MVVM 进行“Master\Detail”数据绑定的问题,希望您能帮助我:
假设我的主模型视图是“ProductModelView”(用于产品实体),并且产品实体有一个名为 ProductInventories 的实体集合类型的属性。我在我的主 ProductModelView 中定义了一个“DomainCollectionView”,名称为:“ProductViewDCV”
我做了以下项目来填充我的主数据网格和详细数据网格:
1-将我的页面的DataContext设置为我的ModelView:ProductModelView
2-将主数据网格(名称:masterDG)绑定到 ProductViewDCV。
3-将详细数据网格(名称:detailDG)绑定到我的主 ProductViewDCV 的 ProductInventories 属性:
<sdk:DataGrid x:Name=" detailDG "
ItemsSource="{Binding Path= ProductViewDCV. ProductInventories }"/>
程序运行良好,当我更改主 DataGrid 中的行时,子 DataGrid 中的当前项也会更改。
但我的问题和问题:
- 当我如步骤 3 中所述绑定我的详细(子)DataGrid 时,子 DataGrid 绑定到“EntityCollection”而不是“PagedCollectionView”或“DomainCollectionView”等视图对象,因此我无法使用排序等好处,我的孩子(详细)DataGrid 上的分组和分页。我想知道:如何将我的子 DataGrid 绑定到 MVVM 模式中的视图对象以使用分页选项。(即我想在我的子 DataGrid 中有分页和分组选项。)
期待收到您的回复。
你好。
非常感谢您的友好和快速的回答。我按照你说的做了,但是遇到了问题,希望你能帮帮我:
方法:
在我的“ProductModelView”(用于产品实体)中,我定义了以下对象:
1-私有DomainCollectionView _ProductViewDCV; (作为主(父)视图)
2-私有PagedCollectionView _ProductViewPCV; (作为详细(子)视图)
3-在“ProductModelView”类的构造函数中,我将一个EVentHandler分配给“_ProductViewDCV”(我的DomainCollectionView作为主视图):
this._ProductViewDCV.CurrentChanged+=_ProductViewDCV_CurrentChanged;
this._ProductViewDCV.Refresh();
4-在 EventHandler 中,我创建一个新的“PagedCollectionView”(我的 Detail/Child 视图)并将其分配给我的 _ProductViewDCV:
if (_ProductViewDCV.CurrentItem != null)
{
_ProductViewPCV = new PagedCollectionView((_ProductViewDCV.CurrentItem as BA1.Web.Product).ProductInventories);
//ProductViewPCV.Refersh();
}
5-在我的视图文件(XAML 文件)中,我有两个数据网格,配置如下:
Page DataContext is set to an object of my ModelView : ProductViewModel
sdk:DataGrid x:Name="dgParent" Grid.Row="1" ItemsSource="{Binding Path=ProductViewDCV}"//my Master/Parent DataGrid
sdk:DataGrid Grid.Row="2" x:Name="dgDetails" ItemsSource="{Binding Path=ProductViewPCV}//My Detail/Child DataGrid
问题:
当我更改 Master/Parent DataGrid 中的当前行时,Detail/Child DataGrid 中没有任何反应,并且测试 Detail/Child DataGrid 的 Itemssource 属性,返回“Null”,而不是“PagedCollectionview”的对象。
我的问题在哪里?为什么 Detail/Child DataGrid 没有更新以响应主/父当前项目的更改? (测试表明,当它的父集合发生变化时,ProductViewPCV 本身也会发生变化。)
再次感谢您就这个问题给我建议。
【问题讨论】:
-
我现在对 Silverlight 有点生疏了,但我怀疑您必须将视图模型中的子集合公开为 PagedCollectionView,并在父集合视图上的当前项目时填充它变化。
标签: silverlight