当前位置: 首页 > news >正文

WPF---数据模版

教程:微软文档

1.定义

数据模版就是数据的外衣,这个外衣的颜色,展示的内容和形式,可以由用户自定义。即数据的表象形式。

2.常用知识

2.1 数据模版的定义

  • 指明Key的定义
<DataTemplate x:Key="taskDataTemplate"><StackPanel Margin="5" Orientation="Horizontal"><CheckBox Margin="5,0,10,0" IsChecked="{Binding IsDone}" /><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Name}" /></StackPanel>
</DataTemplate>
  • 指明类的定义
<DataTemplate DataType="{x:Type local:Task}"><StackPanel Margin="5" Orientation="Horizontal"><CheckBox Margin="5,0,10,0" IsChecked="{Binding IsDone}" /><Border x:Name="border" BorderBrush="Yellow" BorderThickness="3"><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Description}" /></Border></StackPanel><DataTemplate.Triggers><DataTrigger Binding="{Binding TaskType}"><DataTrigger.Value><local:TaskType>home</local:TaskType></DataTrigger.Value><Setter TargetName="border" Property="Background" Value="LightGreen" /></DataTrigger></DataTemplate.Triggers>
</DataTemplate>

注意: 此 DataTemplate 会自动应用于所有 Task 对象。 请注意,在这种情况下,x:Key 是隐式设置的。 因此,如果为此 DataTemplate 分配 x:Key 值,你将替代隐式 x:Key,并且不会自动应用 DataTemplate。

2.2 数据模版的DataTrigger

同上

2.3 为数据选择数据模版

  1. 写一类继承自 DataTemplateSelector 并重写方法 SelectTemplate
public class TaskListDataTemplateSelector : DataTemplateSelector
{public override DataTemplate SelectTemplate(object item, DependencyObject container){FrameworkElement element = container as FrameworkElement;if (element != null && item != null && item is Task){Task taskitem = item as Task;if (taskitem.Priority == 1)returnelement.FindResource("importantTaskTemplate") as DataTemplate;elsereturnelement.FindResource("taskDataTemplate") as DataTemplate;}return null;}
}
  1. 在资源里面引用这个数据模版选择器
 <local:TaskListDataTemplateSelector x:Key="taskSelector"/>
  1. 在使用数据模版的控件里面使用属性:ItemTemplateSelector
        <ListBox ItemsSource="{Binding Source={StaticResource myTodoList}}" ItemTemplateSelector="{StaticResource taskSelector}" />

符完整实例

<Windowx:Class="Study04_数据模版.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:local="clr-namespace:Study04_数据模版"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="MainWindow"Width="800"Height="450"mc:Ignorable="d"><Window.Resources><local:Tasks x:Key="myTodoList" /><DataTemplate x:Key="taskDataTemplate"><StackPanel Margin="5" Orientation="Horizontal"><CheckBox Margin="5,0,10,0" IsChecked="{Binding IsDone}" /><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Name}" /></StackPanel></DataTemplate><DataTemplate x:Key="importantTaskTemplate"><StackPanel Margin="5" Orientation="Vertical"><CheckBox Margin="5,0,10,0" IsChecked="{Binding IsDone}" /><Border BorderBrush="Red" BorderThickness="2" ><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Name}" /></Border><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Description}" /></StackPanel></DataTemplate><DataTemplate DataType="{x:Type local:Task}"><StackPanel Margin="5" Orientation="Horizontal"><CheckBox Margin="5,0,10,0" IsChecked="{Binding IsDone}" /><Border x:Name="border" BorderBrush="Yellow" BorderThickness="3"><TextBlock VerticalAlignment="Center" FontSize="16" Text="{Binding Description}" /></Border></StackPanel><DataTemplate.Triggers><DataTrigger Binding="{Binding TaskType}"><DataTrigger.Value><local:TaskType>home</local:TaskType></DataTrigger.Value><Setter TargetName="border" Property="Background" Value="LightGreen" /></DataTrigger></DataTemplate.Triggers></DataTemplate><local:TaskListDataTemplateSelector x:Key="taskSelector"/></Window.Resources><Grid><!--<ListBox Margin="10,10,10,10" ItemTemplate="{Binding Source={StaticResource taskDataTemplate}}" ItemsSource="{Binding Source={StaticResource myTodoList}}" />--><!--<ListBox Margin="10,10,10,10" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource myTodoList}}" />--><ListBox ItemsSource="{Binding Source={StaticResource myTodoList}}" ItemTemplateSelector="{StaticResource taskSelector}" /></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;namespace Study04_数据模版
{public class Tasks : ObservableCollection<Task>{public Tasks(){Add(new Task { Name = "Task 1", Description = "Description for Task 1", Priority = 1, TaskType = TaskType.work });Add(new Task { Name = "Task 2", Description = "Description for Task 2", Priority = 2, TaskType = TaskType.home });Add(new Task { Name = "Task 3", Description = "Description for Task 3", Priority = 3, TaskType = TaskType.work });}}public class Task{public string Name { get; set; }public string Description { get; set; }public int Priority { get; set; }public TaskType TaskType { get; set; }public override string ToString(){return Description.ToString();}}public enum TaskType{home,work,}public class TaskListDataTemplateSelector : DataTemplateSelector{public override DataTemplate SelectTemplate(object item, DependencyObject container){FrameworkElement element = container as FrameworkElement;if (element != null && item != null && item is Task){Task taskitem = item as Task;if (taskitem.Priority == 1)returnelement.FindResource("importantTaskTemplate") as DataTemplate;elsereturnelement.FindResource("taskDataTemplate") as DataTemplate;}return null;}}
}

3 其它示例

3.1 示例一

  1. 定义数据模版
<DataTemplate x:Key="ControlATemplate"><local:RecipeInfoUC DataContext="{Binding DataContext.Parameter, RelativeSource={RelativeSource AncestorType=ContentControl}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate><!--  控件B的模板 - 当IsRework为True时使用  -->
<DataTemplate x:Key="ControlBTemplate"><local:RecipeReworkUC DataContext="{Binding DataContext.Parameter, RelativeSource={RelativeSource AncestorType=ContentControl}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
  1. 定义数据模版选择器
 /// <summary>/// 模板选择器 - 根据IsRework的值选择对应的模板/// </summary>public class ReworkTemplateSelector : DataTemplateSelector{/// <summary>/// 控件A的模板属性/// </summary>public DataTemplate ControlATemplate { get; set; }/// <summary>/// 控件B的模板属性/// </summary>public DataTemplate ControlBTemplate { get; set; }/// <summary>/// 模版选择方法/// </summary>/// <param name="item"></param>/// <param name="container"></param>/// <returns>模版</returns>public override DataTemplate SelectTemplate(object item, DependencyObject container){// 检查item是否为bool类型if (item is bool isRework){// 根据IsRework的值返回对应的模板return isRework ? ControlBTemplate : ControlATemplate;}// 默认返回控件B的模板return ControlBTemplate;}}
  1. 在资源中引用数据模版
<local:ReworkTemplateSelector x:Key="ReworkTemplateSelector" ControlATemplate="{StaticResource ControlATemplate}" ControlBTemplate="{StaticResource ControlBTemplate}" />
  1. 使用数据模版选择器
 <ContentControl Content="{Binding HasReWorkCamera}" ContentTemplateSelector="{StaticResource ReworkTemplateSelector}" />
http://www.lryc.cn/news/624955.html

相关文章:

  • 算法题打卡力扣第26. 删除有序数组中的重复项(easy))
  • 计算机网络-IPv6
  • 使用 uv管理 Python 虚拟环境:比conda更快、更轻量的现代方案
  • 保姆级教学:使用 Jenkins 部署前端项目(2025 年最新版)
  • 知识蒸馏 Jensen-Shannon散度
  • Vue3 全新特性 defineModel 深度解析
  • [Oracle数据库] Oracle 进阶应用
  • BPO(Business Process Optimization,业务流程优化)
  • 信创产业:从技术突围到生态重构的强国之路
  • redis持久化机制:RDB和AOF
  • 手机视频怎么提取音频?3步转成MP3,超简单!
  • 十年回望:Vue 与 React 的设计哲学、演进轨迹与生态博弈
  • PLC 控制系统中 PCB 板的选型与设计要点
  • 申请免费的SSL证书,到期一键续签
  • 深入浅出决策树
  • python学习DAY45打卡
  • JMeter与大模型融合应用之构建AI智能体:评审性能测试脚本
  • Nodejs学习
  • 基于ssm jsp中学校园网站源码和答辩PPT论文
  • 视觉语言导航(11)——预训练范式 4.1
  • 封装、继承、多态的含义及其项目应用
  • 机器人技术核心模块与前沿趋势总结
  • TikTok墨西哥POP店今日正式开放!0佣金+流量扶持+5店开放
  • PG靶机 - Bratarina
  • C# NX二次开发:字符串控件StringBlock讲解
  • Pandas 中常用的统计计算、排序、分组聚合
  • plantsimulation知识点25.8.18-从一个RGV到另一台RGV,工件长度和宽度方向互换
  • 【牛客刷题】计算1到n最低位1代表的数字之和
  • Layui COP证书管理系统
  • 《Image Classification with Classic and Deep Learning Techniques》复现