添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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" />
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.