六、排序

如果想以特定的方式对数据进行排序,可以绑定到 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 = "";

           liang.Name = "梁丘";

           liang.Birthday = "1990-02-03";

           liang.Country = "中国";

           Student zuo = new Student();

           zuo.Age = "";

           zuo.Name = "左丘";

           zuo.Birthday = "1992-02-03";

           zuo.Country = "中国";

           Student diwu = new Student();

           diwu.Age = "";

           diwu.Name = "第五言";

           diwu.Birthday = "1982-11-03";

           diwu.Country = "中国";

           Student yang = new Student();

           yang.Age = "";

           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;

       }

    }

}

WPF入门教程系列十八——WPF中的数据绑定(四)的更多相关文章

  1. WPF入门教程系列十五——WPF中的数据绑定(一)

    使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...

  2. WPF入门教程系列十六——WPF中的数据绑定(二)

    三.绑定模式 通过上一文章中的示例,学习了简单的绑定方式.在这里的示例,要学习一下绑定的模式,和模式的使用效果. 首先,我们来做一个简单示例,这个示例是根据ListBox中的选中项,去改变TextBl ...

  3. WPF入门教程系列十九——ListView示例(一)

    经过前面的学习,今天我做一个比较综合的WPF程序示例,主要包括以下功能: 1) 查询功能.从数据库(本地数据库(local)/Test中的S_City表中读取城市信息数据,然后展示到WPF的Windo ...

  4. WPF入门教程系列十四——依赖属性(四)

    六.依赖属性回调.验证及强制值 我们通过下面的这幅图,简单介绍一下WPF属性系统对依赖属性操作的基本步骤: 借用一个常见的图例,介绍一下WPF属性系统对依赖属性操作的基本步骤: 第一步,确定Base ...

  5. WPF入门教程系列十二——依赖属性(二)

    二. 依赖属性的优先级 由于WPF 允许我们可以在多个地方设置依赖属性的值,所以我们就必须要用一个标准来保证值的优先级别.比如下面的例子中,我们在三个地方设置了按钮的背景颜色,那么哪一个设置才会是最终 ...

  6. WPF入门教程系列十——布局之Border与ViewBox(五)

    九. Border Border 是一个装饰的控件,此控件绘制边框及背景,在 Border 中只能有一个子控件,若要显示多个子控件,需要将一个附加的 Panel 控件放置在父 Border 中.然后可 ...

  7. WPF入门教程系列二十三——DataGrid示例(三)

    DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...

  8. WPF入门教程系列三——Application介绍(续)

    接上文WPF入门教程系列二——Application介绍,我们继续来学习Application 三.WPF应用程序的关闭 WPF应用程序的关闭只有在应用程序的 Shutdown 方法被调用时,应用程序 ...

  9. WPF入门教程系列二——Application介绍

    一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...

随机推荐

  1. java.lang.IllegalStateException: Web app root system property already set to different value

    webAppRootKey是在java web项目的web.xml配置文件中表示项目的唯一标示,在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过log4j日志的方 ...

  2. Python爬虫学习(11):Beautiful Soup的使用

    之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...

  3. ie6,ie7,ie8 css bug兼容解决方法

    IE浏览器以不支持大量的css 属性出名,同时也因其支持的css属性中存在大量bug. 这里收集了好多的bug以及其解决的办法,都在这个文章里面记录下来了!希望以后解决类似问题的时候能够快速解决,也希 ...

  4. 二分 题目 压缩打包 Special Judge? 不不不 当然不是

    http://noi.openjudge.cn/ch0111/ No 题目 分数 01 查找最接近的元素 10 3176 02 二分法求函数的零点 10 2181 03 矩形分割 10 1420 04 ...

  5. Hive文件存储格式

    hive文件存储格式 1.textfile textfile为默认格式   存储方式:行存储   磁盘开销大 数据解析开销大   压缩的text文件 hive无法进行合并和拆分 2.sequencef ...

  6. View的弹性滑动

    View的弹性滑动 实现弹性滑动的思想:将一次大的滑动分成若干次小的滑动并在一个时间段内完成,具体的实现方式有很多,如通过Scroller.Handler#postDelayed以及Thread#sl ...

  7. highchart 添加新的series

    code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" c ...

  8. RunLoop(基本操作)

    基本概念 -(void)runTimerInThread { //NSAutoreleasePool,没的用 [NSTimer scheduledTimerWithTimeInterval:1.0 t ...

  9. Django 搭建简易博客

    新增一个 APP 博客算是一个功能集,因此我们应将其体现为一个模块.这表现在 Django 应用里则是为其创建一个 APP Package.现在让 manage.py 中的 startapp 闪亮登场 ...

  10. Hibernate一对多配置

    刚刚学习了Hibernate框架的基础知识,下面我来说说关于Hibernate一对多的配置 首先是大配置 连接数据库 用户名 和密码 能和小配置连接 部门小配置: 员工小配置: 部门实体类 员工实体类 ...