我在 Silverlight 应用程序中使用 MVVM。因此,我使用 PagedCollectionView 作为我的 View 模型的属性,将其绑定(bind)到 DataGrid ItemSource。但是我有这个要求:“加载用户控件时,网格中的所有组都应该折叠起来。”。当我使用页面 Collection View 时,我使用了这段代码:
this.PinesView = new PagedCollectionView(this.Pines);
PinesView.GroupDescriptions.Add(new PropertyGroupDescription("Operador"));
PinesView.GroupDescriptions.Add(new PropertyGroupDescription("Marca"));
现在我有了折叠组的代码,但我发现的唯一一段代码需要在 UI 上运行,所以很难将它链接到我的 ViewModel,因为该 Collection View 是异步填充的,所以我不这样做知道如何向 UI 传达已填充的集合以运行此代码;或者更好的是,如何将折叠指令从我的 ViewModel 发送到 UI。
你能帮帮我吗?
最佳答案
public View()
{
InitializeComponent();
datagrid.LoadingRowGroup += new EventHandler<DataGridRowGroupHeaderEventArgs>(datagrid_LoadingRowGroup);
}
void datagrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
datagrid.LoadingRowGroup -= datagrid_LoadingRowGroup;
foreach(CollectionViewGroup group in (datagrid.ItemsSource as PagedCollectionView).Groups)
{
datagrid.CollapseRowGroup(group, true);
}
}
关于c# - 如何使用 MVVM 折叠 Silverlight DataGrid 组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3018917/