Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using collectionviewsource to sort the list in the xaml

HTML
<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>


and the above collection resource is bounded to listview as below


HTML
<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>




and in my view model i have an Observable collection property of mylist
C#
public ObservableCollection<MyItem> MyList
        {
            get
            {
                return this.myList;
            }

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


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
Posted
Updated 10-Jun-18 8:58am
v2

Make sure MyItem derives from INotifyPropertyChanged also and that the change to ID triggers a PropertyChanged event.
 
Share this answer
 
Comments
arun_pk 8-Feb-12 4:44am     CRLF
HiYes my ID property also has property changed event public int ID { get { return this.id; } set { if (this.id!= value) { this.id= value; } OnPropertyChanged("ID"); } }
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900