CollectionViewSource没有排序 [英] CollectionViewSource not sorting

查看:162
本文介绍了CollectionViewSource没有排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用collectionviewsource对xaml中的列表进行排序



I am using collectionviewsource to sort the list in the xaml

<CollectionViewSource x:Uid="CollectionViewSource_1" x:Key='sortedList'

                              Source="{Binding Path=MyList}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription x:Uid="scm:SortDescription_1" PropertyName="ID"

                                     Direction="Ascending" />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>





以上收集资源限制在listview,如下所示







and the above collection resource is bounded to listview as below


<ListView x:Uid="myistBox" Name="myListBox" ItemsSource="{Binding Source={StaticResource sortedList}}"
                        SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                        Margin="5,10,5,5"
                        Grid.Row="1"
                        extenders:ListViewExtenders.AutoScrollToEnd="True"
                        SelectionMode="Single">
              <ListView.View>
                  <GridView x:Uid="GridView_1">
                      <GridViewColumn x:Uid="GridViewColumn_1" DisplayMemberBinding="{Binding Path=ID}" Header="ID" />
                      <GridViewColumn x:Uid="GridViewColumn_2" DisplayMemberBinding="{Binding Path=Name}" Header="Name"/>
                  </GridView>
              </ListView.View>
          </ListView>









在我的视图模型中我有一个可观察的mylist集合属性





and in my view model i have an Observable collection property of mylist

public ObservableCollection<MyItem> MyList
        {
            get
            {
                return this.myList;
            }

            set
            {
                if (this.myList != value)
                {
                    this.myList = value;
                    OnPropertyChanged("MyList");
                }
            }
        }





和Myitem类包含两个属性

ID和名称



因此在我的代码中,当我更改ID时...我希望列表被排序,但这不会发生..请参阅建议我的实现有什么问题



and the Myitem class consist of two properties
ID and Name

so in my code when i change the id .. i am expecting the List to be sorted but this is not happening .. pls advice what is wrong in my implementation

推荐答案

确保MyItem也来自INotifyPropertyChanged,并且对ID的更改会触发PropertyChanged事件。
Make sure MyItem derives from INotifyPropertyChanged also and that the change to ID triggers a PropertyChanged event.


对于有这个问题并且通过Google发现这个超级老帖子的其他人:



对于实时排序,除了设置SortDescriptions之外,你还必须添加LiveSortingProperties(它使用一个或多个字符串,使用WindowsBase程序集中的System命名空间),并设置IsLiveSortingRequested = True。



For anyone else that has this issue and comes across this super-old post via Google:

For "live" sorting, in addition to setting up the SortDescriptions, you must also add LiveSortingProperties (which takes one or more strings, using the System namespace from WindowsBase assembly), and set IsLiveSortingRequested = True.

xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"


<CollectionViewSource x:Key="ChannelsToAddView" x:Name="ChannelsToAddView" Source="{Binding DiscordChannels}"
                              Filter="ChannelsToAddView_Filter" IsLiveSortingRequested="True">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="DiscordChannelName" />
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.LiveSortingProperties>
                <s:String>DiscordChannelName</s:String>
            </CollectionViewSource.LiveSortingProperties>
        </CollectionViewSource>


这篇关于CollectionViewSource没有排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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