views:

1588

answers:

2

First off let me just say I'm very new to coding so there are big gaps in my knowledge... anywho:

Right, I'm trying to sort a WPF listbox when a button is clicked, preferrably in pure xaml (otherwise VB). I'm having a hard time seeing as most samples are written in C#. Here's 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, what I want to do is add a button like so:

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

Now when this button is clicked, I'd like the listbox to sort by the field "First Name", I assume I have to change the sort description somehow, so could anyone tell me how please? Or am i going about this hte worng way. Again preferabbly in XAML, but if need be in VB could you try and keep it simple please??

Thanks guys

+1  A: 

Hope it helps: Google came up with this (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();

Regarding Example 3 this should be correct:

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

thanks... I've seen some samples like this but the problem is that they are written in C#, and my project is in VB.NET, which few examples seem to exist for >_<

Come on, you can convert 5 lines of code from C# to VB, can't you?
Gustavo Cavalcanti