【问题标题】:Sorting a PagedCollectionView by index (using Silverlight Datagrid)按索引对 PagedCollectionView 进行排序(使用 Silverlight Datagrid)
【发布时间】:2010-07-09 08:26:41
【问题描述】:

我有一个 silverlight 数据网格,它绑定到一个显示 RowViewModel 集合的 PagedCollectionView。

每个 RowVM 都有一个 CellViewModels 集合,datagrid 列是模板列,动态生成,其内容绑定到 Cell[0].Content、Cell[1].Content 等。这是因为 rowviewmodels 是从一项服务,可以包含任意数量的列和不同类型的内容。

这很好用,但是在启用数据网格中的列排序时遇到了问题。 DataGridColumns 上的 SortMemberPath 属性(最终成为 SortDescription.PropertyName)似乎不适用于包含索引的表达式,例如“Cells[1].Content”。

有人知道解决这个问题的方法吗?

【问题讨论】:

    标签: wpf silverlight silverlight-toolkit


    【解决方案1】:

    您可以根据 CellViewModels 的集合生成动态对象

    看到这个

    Lightweight DataTable for your Silverlight applications

    最好的问候。

    布鲁诺·罗查

    【讨论】:

    • 嗨!感谢您的回复,但我不确定这与我对 silverlight 工具包数据网格进行排序的问题有何关系?
    【解决方案2】:

    在这里回答我自己的问题。

    我通过将生成的列上的 SortMemberPath 设置为它们在 RowVM.Cells 集合中的索引号解决了这个问题,并向我的 RowVM 添加了一个 SortOnMe 属性。

    当用户对数据网格进行排序时,它会将包含索引号(取自 SortMemberPath)的 SortDescription 添加到 PagedCollectionView。我通过以下方法监听 PagedCollectionView 上的 propertychanged 事件来监控这一点。它添加了一个新的 SortDescription,告诉 PagedCollectionView 在“SortOnMe”上排序,并将数据从有问题的单元格复制到 row.SortOnMe。

    private bool _currentlySorting;
    private void PagedCollectionView_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var pcv = (PagedCollectionView)sender;
        int columnIndex = 0;
        if (!_currentlySorting && e.PropertyName == "SortDescriptions" && pcv.SortDescriptions.Count == 1 && int.TryParse(pcv.SortDescriptions[0].PropertyName, out columnIndex)) {
            _currentlySorting = true; 
            pcv.SortDescriptions.Add(new SortDescription("SortOnMe", pcv.SortDescriptions(0).Direction));
            foreach (RowViewModel row in pcv.SourceCollection) {
                row.SortOnMe = row.Cells(columnIndex).Content;
            }
            _currentlySorting = false;
        }
    }
    

    说实话,这是一个非常丑陋的解决方案。但它有效,而且我现在花了太多时间把头撞在墙上。

    由于 PagedCollectionView 是一个密封类(为什么?!),我能想到的唯一其他方法是创建我自己的 PagedCollectionView 并在那里处理排序。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      相关资源
      最近更新 更多