3

我有一个使用实体框架查询数据库并将结果放在ICollectionView. ICollectionView充当ItemsSourcea的DataGrid。在第一次查询时一切正常,但在第二次查询时,尽管应用了正确的SortDescriptions.

这是我尝试查询和分组/排序数据的代码:

    CollectionViewSource cvsRS;

    private ObservableCollection<productorder> rs;
    public ObservableCollection<productorder> RS
    {
        get { return rs; }
        set
        {
            if (rs != value)
            {
                rs = value;
                OnPropertyChanged("RS");
            }
        }
    }

    private ICollectionView rsView;
    public ICollectionView RSView
    {
        get { return rsView; }
        set
        {
            if (rsView != value)
            {
                rsView = value;
                OnPropertyChanged("RSView");
            }
        }
    }

    public void QueryDatabase()
    {

        RS = new ObservableCollection<productorder>(DatabaseEntities.productorders.Where(o => o.month.id == CurrentMonth.id));
        if (RS != null)
        {
            cvsRS.Source = RS;
            RSView = cvsRS.View;

            RSView.GroupDescriptions.Clear();
            RSView.GroupDescriptions.Add(new PropertyGroupDescription("producttype.productcategory.name"));
            RSView.GroupDescriptions.Add(new PropertyGroupDescription("producttype.name"));  

            RSView.SortDescriptions.Clear();
            RSView.SortDescriptions.Add(new SortDescription("producttype.productcategory.sortorder", ListSortDirection.Ascending));
            RSView.SortDescriptions.Add(new SortDescription("client.name", ListSortDirection.Ascending));
            RSView.Refresh();
            CurrentRecord = null;
            SelectedRecords = null;
        }
    }

分组工作正常,但根据排序,组的顺序不正确。我已经尝试了许多可能的“修复”但没有成功(例如,将排序/组描述直接添加到CollectionViewSource,在分组之前排序,删除一些排序/分组,删除SortDescriptions每个CollectionViewSource 不会在属性更改时重新排序) .

无论执行多少查询,有谁知道如何维护排序顺序?我愿意接受在DataGrid可能有效的情况下查询显示数据的替代方法。

4

1 回答 1

2

尝试将您的财产绑定CollectionViewSource.Source到您的ObservableCollection<T>财产。在 viewmodel 构造函数中设置绑定。然后,别管它。更新ObservableCollection<T>,替换它等。只要它是一个ObservableCollection<T>并且每当你替换它时它的公共属性就会提高PropertyChanged,整个事情都会起作用。

public MyViewModel()
{
    BindCollectionViewSource();
}

protected void BindCollectionViewSource()
{
    cvsRS = new CollectionViewSource();

    var binding = new Binding
    {
        Source = this,
        Path = new PropertyPath("RS")
    };
    BindingOperations.SetBinding(cvsRS, CollectionViewSource.SourceProperty, binding);
}

//  Since we're not going to be messing with cvsRS or cvsRS.View after the 
//  constructor finishes, RSView can just be a plain getter. The value it returns 
//  will never change. 
public ICollectionView RSView
{
    get { return cvsRS.View; }
}

您不能只将绑定分配给Source; 不仅如此。您在 XAML 中看到的Source="{Binding RSView}"内容可能看起来像一个作业,但为方便起见,隐藏了一些细节。Binding积极做事。它需要知道目标对象是谁。

我确实看到了一件有趣的事情:我给了我的测试代码一个PropertyGroupDescription和一个SortDescription。当我将项目添加到集合中时,它会在组中对它们进行排序。然后当我调用 RSView.Refresh() 时,它在不参考组的情况下使用它们。不知道我明白它在那里做什么。

于 2016-10-31T15:15:45.173 回答