【问题标题】:PagedCollectionView groupings don't seem to automatically be refreshed when a new element is added to the source collection将新元素添加到源集合时,PagedCollectionView 分组似乎不会自动刷新
【发布时间】:2010-09-23 19:40:30
【问题描述】:

我在 Silverlight 应用程序中使用 PagedCollectionViewObservableCollection<T> 绑定到 DataGrid。在这种情况下,源集合在其生命周期内可能会发生无限数量的更新。但是,似乎如果我使用PropertyGroupDescriptionDataGrid 中的元素进行分组,那么每次使用不适合的元素更新源集合时,我都需要使用PagedCollectionView.GroupDescriptions.Add(...) 重新应用该分组到现有的分组中。有没有办法让分组自动刷新/重新计算?

    public ObservableCollection<DataItem> DataItems
    {
        get         { return _dataItems;  }
        private set { _dataItems = value; }
    }

    public PagedCollectionView ItemsView
    {
        get         { return _itemsView;  }
        private set { _itemsView = value; }
    }

    public MyGridControl()
    {
        // Initialize data members
        DataItems = new ObservableCollection<DataItem>();
        ItemsView = new PagedCollectionView(GridDataItems);

        Loaded += new RoutedEventHandler(MyGridControl_Loaded);
    }

    private void MyGridControl_Loaded(object sender, RoutedEventArgs e)
    {
        _myGrid.ItemsSource = ItemsView;
    }

    public void RegisterDataItem(DataItem item)
    {
        DataItems.Add(item);
        /* To get grouping to work like I want, I have to add this:
         * ItemsView.GroupDescriptions.Clear();
         * ItemsView.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));                     
         */            
    }

    public void UnregisterError(DataItem item)
    {
        DataItem.Remove(item);            
    }

谢谢,如果我可以提供任何其他信息,请告诉我。

【问题讨论】:

    标签: c# .net wpf silverlight wpfdatagrid


    【解决方案1】:

    因此,据我所知,分组操作似乎是对数据执行的一次性操作,因为它存在于操作 PagedCollectionView.GroupDescriptions 集合时。似乎确实需要在更改源集合时重新应用所需的分组。

    我确实为我的具体情况找到了一种替代方法。由于我将 ObservableCollection 用于视图的源集合,因此我可以将某些东西连接到该源集合的 CollectionChanged 事件,其中 PagedCollectionView.GroupDescriptions 集合被清除,然后重新应用所需的 GroupDescription。这似乎不完全符合良好的 OO 实践,如果视图的源集合没有实现 INotifyCollectionChanged,它也不可用。

    如果有人可以提供另一种方法,我将把它留得更久一点,否则我就让步了。

    再次感谢。

    【讨论】:

      【解决方案2】:

      查看 Reflector 中的代码,分组正确处理,但如果您在 DataGridIsReadOnly == true 中使用视图,则在更改源集合时不会看到分组反映。

      【讨论】:

      • 我没有使用 IsReadOnly。一定有别的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 2020-06-09
      • 1970-01-01
      • 2017-03-20
      相关资源
      最近更新 更多