数据模板选择器

        以DataGrid中单元格模板选择器为例进行说明。第一列显示姓名,第二列根据身高选择不同的数据模板,身高低于160时,单元格显示红色背景;不低于160时,显示绿色。效果如图:

1、构造后台数据

    public class Student
    {
        private string _name;
        private UInt16 _height;

        public String Name 
        {
            get => _name;
            set => _name = value;
        }

        public UInt16 Height
        {
            get => _height;
            set => _height = value;
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Student> _students = new();

        public MainWindow()
        {
            InitializeComponent();

            Students.Add(new Student() { Name = "小宏", Height = 162 });
            Students.Add(new Student() { Name = "小强", Height = 161 });
            Students.Add(new Student() { Name = "小红", Height = 158 });
            Students.Add(new Student() { Name = "小云", Height = 164 });

            this.StudentDataGrid.ItemsSource = Students;
        }

        public ObservableCollection<Student> Students
        {
            get => _students;
        }
    }

 2、编写前台代码

    <Window.Resources>
        <DataTemplate x:Key="Qualified">
            <TextBlock Text="{Binding Height}" Background="Green"></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="Unqualified">
            <TextBlock Text="{Binding Height}" Background="Red"></TextBlock>
        </DataTemplate>
    </Window.Resources>

    <DataGrid x:Name="StudentDataGrid" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="姓名" Binding="{Binding Name}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="身高">
                <DataGridTemplateColumn.CellTemplateSelector>
                    <local:HeightSelector QualifiedTemplate="{StaticResource Qualified}"  UnqualifiedTemplate="{StaticResource Unqualified}"></local:HeightSelector>
                </DataGridTemplateColumn.CellTemplateSelector>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

3、创建选择器

    public class HeightSelector : DataTemplateSelector
    {
        public DataTemplate QualifiedTemplate { get; set; }
        public DataTemplate UnqualifiedTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Student stu = item as Student;
            if (stu != null)
            {
                if (stu.Height >= 160)
                {
                    return QualifiedTemplate;
                }
                else 
                {
                    return UnqualifiedTemplate;
                }
            }

            return base.SelectTemplate(item, container);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值