Sort WPF ListBox on button click?

First of all, let me say that I am very new to coding, so there are big gaps in my knowledge ... anywho:

That's right, I am trying to sort the WPF list at the click of a button, preferably in pure xaml (aka VB). It's hard for me to see how most samples are written in C #. Here is my code:

 <Grid.Resources>
      <CollectionViewSource x:Key="myCollectionView"
                            Source="{Binding Path=Query4, Source={x:Static Application.Current}}" >
           <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="ContactID"
                                     Direction="Descending"/>
           </CollectionViewSource.SortDescriptions>
      </CollectionViewSource>
</Grid.Resources>

<ListBox x:Name="ContDefault"
         IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding Source={StaticResource myCollectionView}}"
         ItemTemplate="{StaticResource ContactsList}" />

Now I want to add a button like this:

 <Button x:Name="SortNameAsc"
         Content="Sort By Name"
         Visibility="Visible">

Now that this button is pressed, I would like the list to be sorted by the "Name" field, I assume that I need to somehow change the sort description, so can someone tell me how to do this? Or am I going about this way. Again, preferable in XAML, but if you need to be in VB, you can try and keep it simple, please.

Thanks guys,

+3
1

, : Google (http://www.kudzuworld.com/blogs/Tech/20070815A.en.aspx)

ListCollectionView view = new ListCollectionView(channel.Members);
view.SortDescriptions.Add(new System.ComponentModel.SortDescription("lastName",
  System.ComponentModel.ListSortDirection.Ascending);
view.SortDescriptions.Add(new System.ComponentModel.SortDescription("firstName",
  System.ComponentModel.ListSortDirection.Ascending); 
view.CustomSort = new IComprarerImplementation; //Do this if you want a custom sort;
view.Refresh();

3 :

<ListBox x:Name="ContDefault"
         IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding Source={StaticResource myCollectionView}}"
         ItemTemplate="{StaticResource ContactsList}"
         SortDescription="First Name" />
+2

Source: https://habr.com/ru/post/1706268/


All Articles