C# wpf中的数据绑定4-4(17)

如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider。CollectionViewSource 则会成为数据源,并充当截取 ObjectDataProvider 中的数据的媒介,并提供排序、分组和筛选功能,然后将它传送到目标。
在这里插入图片描述
主页面前台代码:

<Window x:Class="WpfApppaixu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApppaixu.Services"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <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>

models文件夹下Student.cs 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

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

        }

    }
}
    

servicestudent.cs代码:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApppaixu.Models;
using static WpfApppaixu.Models.Student;

namespace WpfApppaixu.Services
{
    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;

        }
    }
}

测试一下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值