您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
WPF
内容介绍 1、新建WPF工程 2、Application介绍 3、Dispatcher介绍 4、Window 介绍 5、主要布局属性介绍 6、Grid网格布局 7、UniformGrid布局 8、DockPanel与ViewBox布局 9、Border与ViewBox布局 10、依赖属性(一) 11、依赖属性(二) 12、依赖属性(三) 13、依赖属性(四) 14、WPF中的数据绑定(一) 15、WPF中的数据绑定(二) 16、WPF中的数据绑定(三) 17、WPF中的数据绑定(四) 18、ListView示例(一) 19、ListView示例(二) 20、DataGrid示例(一) 21、DataGrid示例(二) 22、DataGrid示例(三) 23、引用FontAwesome矢量图 24、ListBox日志效果 25、Polygon绘制多边形 26、Ellipse绘制实心圆 27、数据模板DataTemplate 基于WPF的exe远程升级程序
16、WPF中的数据绑定(三)
18、ListView示例(一)
激萌の小宅 博客园 WPF

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 1

    累计评价

WPF中的数据绑定(四) - (十七)


六、排序

       如果想以特定的方式对数据进行排序,可以绑定到 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 为止。如下图所示。当点击列头时,数据就会进行顺序或逆序排序。如下图。

XAML代码如下:(cs代码在下面有提供)

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        Title="MainWindow" Height="200" Width="300">
    <Grid>
        <StackPanel>
            <StackPanel.Resources>
                <ObjectDataProvider x:Key="students"  ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList">
                </ObjectDataProvider>

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

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

    </Grid>
</Window>


博客园完整代码实例效果如下:(xaml代码有所变动,cs代码通用)


完整代码如下:

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        Title="MainWindow" 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>


Student.cs

using System.Windows;

namespace WpfApp.Services
{
    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); }
        }
    }
}


StudentService.cs

using System.Collections.Generic;

namespace WpfApp.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;
        }
    }
}
16、WPF中的数据绑定(三)
18、ListView示例(一)

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 1

    累计评价

WPF中的数据绑定(四) - (十七)


六、排序

       如果想以特定的方式对数据进行排序,可以绑定到 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 为止。如下图所示。当点击列头时,数据就会进行顺序或逆序排序。如下图。

XAML代码如下:(cs代码在下面有提供)

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        Title="MainWindow" Height="200" Width="300">
    <Grid>
        <StackPanel>
            <StackPanel.Resources>
                <ObjectDataProvider x:Key="students"  ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList">
                </ObjectDataProvider>

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

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

    </Grid>
</Window>


博客园完整代码实例效果如下:(xaml代码有所变动,cs代码通用)


完整代码如下:

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        Title="MainWindow" 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>


Student.cs

using System.Windows;

namespace WpfApp.Services
{
    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); }
        }
    }
}


StudentService.cs

using System.Collections.Generic;

namespace WpfApp.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;
        }
    }
}