若是想以特定的方式對數據進行排序,能夠綁定到 CollectionViewSource,而不是直接綁定到 ObjectDataProvider。CollectionViewSource 則會成爲數據源,並充當截取 ObjectDataProvider 中的數據的媒介,並提供排序、分組和篩選功能,而後將它傳送到目標。app
這個顯示是使用 CollectionViewSource作爲排序的數據源,首先將CollectionViewSource的Source 屬性設置爲 ObjectDataProvider的資源名稱。而後經過設置CollectionViewSource.SortDescriptions屬性,指定排序字段和排序順序:ide
<CollectionViewSource x:Key="studentsView" Source="{Binding Source={StaticResource students}}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Name" Direction="Ascending" />
<scm:SortDescription PropertyName="Age" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
WPF中的DataContext屬性是很是有用的,若是你有多個控件須要綁定同一個數據源,那麼按照WinForm中的作法是給每一個控件都綁定一次數據源,那麼作重複代碼就會不少。而在WPF中你能夠首先把這些須要綁定同一個數據源的控件放在同一個容器控件內,而後將容器控件的 DataContext 設置爲綁定源,容器內的控件的數據源綁定就能夠沒必要再綁定,使用容器的數據源。例如,下面spa
的示例:StackPanel的 DataContext 屬性綁定了數據源,DataGrid就能夠沒必要再次綁定了,直接使用StackPanel綁定的數據源。
code
<StackPanel DataContext="{StaticResource studentsView}">
<TextBlock Width="248" Height="24" Text="數據排序:" TextWrapping="Wrap"/>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="名稱" />
<DataGridTextColumn Binding="{Binding Age}" Header="年齡" />
<DataGridTextColumn Binding="{Binding Country}" Header="國家" />
<DataGridTextColumn Binding="{Binding Birthday}" Header="出生日期" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
若是該容器沒有定義 DataContext,那麼它會繼續查找下一個外部嵌套容器,直到它找到當前的 DataContext 爲止。以下圖所示。當點擊列頭時,數據就會進行順序或逆序排序。以下圖。orm
整個示例的所有代碼以下:xml
<Window x:Class="WpfApp1.WindowBindData" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1.Services" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" Title="WindowBindData" Height="700" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="150"/>
<RowDefinition Height="140"/>
<RowDefinition Height="100*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Width="248" Height="24" Text="股票名稱:" TextWrapping="Wrap"/>
<ListBox x:Name="listStockName" Width="248" Height="56">
<ListBoxItem Content="全通教育"/>
<ListBoxItem Content="大智慧"/>
<ListBoxItem Content="寶鋼股份"/>
<ListBoxItem Content="浦發銀行"/>
<ListBoxItem Content="工商銀行"/>
<ListBoxItem Content="中國建築"/>
<ListBoxItem Content="中國南車"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="你所選中的股票名稱:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listStockName, Path=SelectedItem.Content}">
</TextBlock>
</StackPanel>
<StackPanel Grid.Row="1">
<TextBlock Width="248" Height="24" Text="顏色:" TextWrapping="Wrap"/>
<ListBox x:Name="listColor" Width="248" Height="56">
<ListBoxItem Content="Blue"/>
<ListBoxItem Content="Red"/>
<ListBoxItem Content="Green"/>
<ListBoxItem Content="Gray"/>
<ListBoxItem Content="Cyan"/>
<ListBoxItem Content="GreenYellow"/>
<ListBoxItem Content="Orange"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="改變背景色:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}" Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}">
</TextBlock>
<TextBox Name="txtTwoWay" Text="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}" Background="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Grid.Row="2">
<StackPanel.Resources>
<XmlDataProvider x:Key="MyColors" Source="Colors.xml" XPath="colors">
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Text="XML數據綁定:" TextWrapping="Wrap"/>
<ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}">
</ListBox>
<TextBlock Width="248" Height="24" Text="選中的顏色:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor, Path=SelectedValue, Mode=OneWay}"
>
</TextBlock>
</StackPanel>
<StackPanel Grid.Row="3">
<StackPanel.Resources>
<ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList">
</ObjectDataProvider>
<DataTemplate x:Key="studentLayout" DataType="students">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"/>
<TextBlock Text=", "></TextBlock>
<TextBlock Text="{Binding Path=Age}"></TextBlock>
<TextBlock Text=", "></TextBlock>
<TextBlock Text="{Binding Path=Birthday}"></TextBlock>
<TextBlock Text=", "></TextBlock>
<TextBlock Text="{Binding Path=Country}"></TextBlock>
</StackPanel>
</DataTemplate>
<CollectionViewSource x:Key="studentsView" Source="{Binding Source={StaticResource students}}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Name" Direction="Ascending" />
<scm:SortDescription PropertyName="Age" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</StackPanel.Resources>
<TextBlock Width="248" Height="24" Text="對象數據綁定:" TextWrapping="Wrap"/>
<ListBox x:Name="listObjectBind" Width="450" Height="80" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource students}}" ItemTemplate="{DynamicResource studentLayout}">
</ListBox>
<TextBlock Width="248" Height="24" Text="數據排序:" TextWrapping="Wrap"/>
<DataGrid DataContext="{StaticResource studentsView}" AutoGenerateColumns="False" ItemsSource="{Binding}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="名稱" />
<DataGridTextColumn Binding="{Binding Age}" Header="年齡" />
<DataGridTextColumn Binding="{Binding Country}" Header="國家" />
<DataGridTextColumn Binding="{Binding Birthday}" Header="出生日期" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace WpfApp1.Models { public class Student : DependencyObject { //聲明一個靜態只讀的DependencyProperty字段
public static readonly DependencyProperty NameProperty; public static readonly DependencyProperty AgeProperty; public static readonly DependencyProperty BirthdayProperty; public static readonly DependencyProperty CountryProperty; static Student() { //註冊咱們定義的依賴屬性Name,Age,birthday,Country
NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student), new PropertyMetadata("名稱", OnValueChanged)); AgeProperty = DependencyProperty.Register("Age", typeof(string), typeof(Student), new PropertyMetadata("年齡", OnValueChanged)); BirthdayProperty = DependencyProperty.Register("Birthday", typeof(string), typeof(Student), new PropertyMetadata("出生日期", OnValueChanged)); CountryProperty = DependencyProperty.Register("Country", typeof(string), typeof(Student), new PropertyMetadata("國家", OnValueChanged)); } private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { //當值改變時,咱們能夠在此作一些邏輯處理
} //屬性包裝器,經過它來讀取和設置咱們剛纔註冊的依賴屬性
public string Name { get { return (string)GetValue(NameProperty); } set { SetValue(NameProperty, value); } } public string Age { get { return (string)GetValue(AgeProperty); } set { SetValue(AgeProperty, value); } } public string Birthday { get { return (string)GetValue(BirthdayProperty); } set { SetValue(BirthdayProperty, value); } } public string Country { get { return (string)GetValue(CountryProperty); } set { SetValue(CountryProperty, value); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WpfApp1.Models; namespace WpfApp1.Services { public class StudentService { public List<Student> GetStudentList() { Student liang = new Student(); liang.Age = "18"; liang.Name = "梁丘"; liang.Birthday = "1990-02-03"; liang.Country = "中國"; Student zuo = new Student(); zuo.Age = "22"; zuo.Name = "左丘"; zuo.Birthday = "1992-02-03"; zuo.Country = "中國"; Student diwu = new Student(); diwu.Age = "32"; diwu.Name = "第五言"; diwu.Birthday = "1982-11-03"; diwu.Country = "中國"; Student yang = new Student(); yang.Age = "12"; yang.Name = "羊舌微"; yang.Birthday = "2002-11-13"; yang.Country = "中國"; List<Student> personList = new List<Student>(); personList.Add(liang); personList.Add(zuo); personList.Add(diwu); personList.Add(yang); return personList; } } }