文章出處

六、排序

     如果想以特定的方式對數據進行排序,可以綁定到 CollectionViewSource,而不是直接綁定到 ObjectDataProvider。CollectionViewSource 則會成為數據源,并充當截取 ObjectDataProvider 中的數據的媒介,并提供排序、分組和篩選功能,然后將它傳送到目標。

     這個顯示是使用 CollectionViewSource做為排序的數據源,首先將CollectionViewSource的Source 屬性設置為 ObjectDataProvider的資源名稱。然后通過設置CollectionViewSource.SortDescriptions屬性,指定排序字段和排序順序:

 <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 設置為綁定源,容器內的控件的數據源綁定就可以不必再綁定,使用容器的數據源。例如,下面

的示例:StackPanel的 DataContext 屬性綁定了數據源,DataGrid就可以不必再次綁定了,直接使用StackPanel綁定的數據源。 

<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 為止。如下圖所示。當點擊列頭時,數據就會進行順序或逆序排序。如下圖。

 

整個示例的全部代碼如下:

 

<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;

       }

 

    }

}

 

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()