WPF Learning Notes

Source: Internet
Author: User

8. Collection view when binding to a collection object, WPF always provides a view by default (CollectionViewSource). The view is associated to the source collection and automatically displays the associated action on the target object.

(1) Sorting

Inserting one or more sort criteria (sortdescription) into the Collectionviewsource.sortdescriptions property enables single or multiple conditional sorting.

Window1.xaml

<window x:class= "Learn.WPF.Window1"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:my= "CLR-NAMESPACE:LEARN.WPF"
title= "Window1" >
<Window.Resources>
<my:personallist x:key= "Personals" >
<my:personal name= "Tom" age= "sex=" Male "/>"
<my:personal name= "Mary" age= "one" sex= "Female"/>
<my:personal name= "Jack" age= "sex=" Male "/>"
</my:PersonalList>
</Window.Resources>
<Grid>
<stackpanel datacontext= "{StaticResource personals}" >
<listbox x:name= "ListBox1" Itemssource= "{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding path=name}"/>
<TextBlock>,</TextBlock>
<textblock text= "{Binding path=age}"/>
<TextBlock>,</TextBlock>
<textblock text= "{Binding path=sex}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>

Window1.xaml.cs

public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
    var personals = this.FindResource("personals");
    var view = CollectionViewSource.GetDefaultView(personals);
    view.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Ascending));
  }
}

Changes to the collectionviewsource.sortdescriptions will be directly reflected in the interface display.

protected void ButtonClick(object sender, RoutedEventArgs e)
{
  var personals = this.FindResource("personals");
  var view = CollectionViewSource.GetDefaultView(personals);
  var direction = sender == btnDesc ? ListSortDirection.Descending : ListSortDirection.Ascending;
  view.SortDescriptions.Clear();
  view.SortDescriptions.Add(new SortDescription("Age", direction));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.