排序后WPF数据网格刷新不起作用 [英] WPF datagrid refresh after sorting not working

查看:130
本文介绍了排序后WPF数据网格刷新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对绑定到WPF数据网格的List <>进行排序.第一次加载时,它完全没有排序,但是当您单击标题上的一次时,它应该在升序和降序之间切换.奇怪的是,List<>进行了排序,当我将List<>重新绑定到Itemssource并刷新等时,它仍然显示升序.但是,当我设置一个断点并查看ItemsSource中的内容时,项目是否已排序?!无论出于什么原因,它都不会显示在数据网格中.关于这怎么发生的任何想法?

I'm trying to sort a List<> that is binded to a WPF datagrid. On first load, it's totally unsorted, but when you click once on the header, it should switch between ascending and descending. The weird thing is, the List<> sorts, and when I re-bind the List<> to the Itemssource, and refresh, etc... It still shows the ascending order. But when I set a breakpoint and go see what's in the ItemsSource, the items ARE sorted?! It just doesn't show in the datagrid for whatever reason. Any idea on how this can happen?

DataGrid(LibraryView)的排序事件

SortingEvent of DataGrid (LibraryView)

private void LibraryView_Sorting(object sender, DataGridSortingEventArgs e)
        {  
            var sortDirection = e.Column.SortDirection;
            switch (sortDirection)
            {
                default: 
                case ListSortDirection.Descending:  //not needed, but makes things clearer
                    sortDirection = ListSortDirection.Ascending;
                    break;
                case ListSortDirection.Ascending:
                    sortDirection = ListSortDirection.Descending;
                    break;
            }

            _manager.SortLibrary(e.Column.SortMemberPath, sortDirection);
            //LibraryView.Items.Clear(); //tried this
            LibraryView.ItemsSource = null; //tried this
            LoadLibrary();
            LibraryView.Items.Refresh(); //tried this
        }

LoadLibrary:

LoadLibrary:

private void LoadLibrary()
{
    if (_manager.CheckLibrary())
    {
        LibraryView.ItemsSource = _manager.GetLibrarySongs();
    }
}

排序本身:

public void SortLibrary(string member, ListSortDirection? sortDirection)
    {
    PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(Song)).Find(member, true);

    switch (sortDirection)
    {
        default:
        case ListSortDirection.Descending: //not needed, but makes things clearer
        _library.Songs = _library.Songs.OrderByDescending(s => prop.GetValue(s)).ToList();
        Debug.WriteLine("Sorting descending!!!!");
        break;
        case ListSortDirection.Ascending:
        _library.Songs = _library.Songs.OrderBy(s => prop.GetValue(s)).ToList();
        Debug.WriteLine("Sorting ascencding!!!!");
        break;
   }
  }

我知道有很多主题,但是我遇到的所有问题仍然无法解决. 我没有使用WPF的丰富经验,因此,如果我做错了任何事情或不当行为,请告诉我. 预先感谢!

I know there are tons of topics on this, but everything I came across, still doesn't fix this. I don't have much experience with WPF so if I'm doing anything wrong, or bad practice, please let me know. Thanks in advance!

推荐答案

单击列标题时,DataGridList<T>的视图进行排序,而无需任何操作.这是内置功能,因此您根本不必处理Sorting事件.只需将ItemsSource设置或绑定到您的List<T>.

The DataGrid sorts the view of the List<T> when you click on the column headers without you having to anything at all. This is built-in functionality so you don't have to handle the Sorting event at all. Just set or bind the ItemsSource to your List<T>.

请注意,实际的源集合(即List<T>)并未排序.每当您绑定到WPF中的某个集合时,都会自动创建一个ICollectionView,并且该集合按DataGrid进行排序.

Note that the actual source collection, i.e. the List<T> is not sorted though. Whenever you bind to some collection in WPF, an ICollectionView is automatically created and it is this one that gets sorted by the DataGrid.

这篇关于排序后WPF数据网格刷新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆