当前位置:网站首页>【WPF】数据模板选择器DataTemplateSelector
【WPF】数据模板选择器DataTemplateSelector
2022-04-21 06:34:00 【阿月浑子2021】
作用:为数据模型内不同的属性值应用不同的数据模板,多用于容器
以listbox为例:
<ListBox x:Name="_list" ItemsSource="{Binding Model}"/>
public partial class MainWindow : Window
{
public List<Student> StuList { get; set; } = new List<Student>();
public MainWindow()
{
InitializeComponent();
_list.DataContext = new { Model= StuList };
StuList.Add(new Student("张三", 60));
StuList.Add(new Student("张四", 70));
StuList.Add(new Student("张五", 80));
StuList.Add(new Student("张六", 90));
StuList.Add(new Student("张一", 40));
StuList.Add(new Student("张二", 100));
}
}
public class Student
{
public Student(string name,int score)
{
Name = name;
Score = score;
}
public string Name { get; set; }
public int Score { get; set; }
}
创建 TemplateSelector 类继承自 DataTemplateSelector:
public class TemplateSelector:DataTemplateSelector
{
public DataTemplate HighTemplate { get; set; }
public DataTemplate LowTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item == null) return null;
var stu =item as Student;
if (stu.Score > 60) return HighTemplate;
return LowTemplate;
}
}
定义模板样式:
<Window.Resources>
<DataTemplate x:Key="highTemplate">
<StackPanel Background="Green" Orientation="Horizontal" Margin="10">
<TextBlock Text="{Binding Name}" FontSize="30"/>
<TextBlock Text="通过啦!!" FontSize="30"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="lowTemplate">
<StackPanel Background="Red" Orientation="Horizontal" Margin="10">
<TextBlock Text="{Binding Name}" FontSize="30"/>
<TextBlock Text="不及格!!" FontSize="30"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
应用:
<ListBox x:Name="_list" ItemsSource="{Binding Model}">
<ListBox.ItemTemplateSelector>
<local:TemplateSelector HighTemplate="{StaticResource highTemplate}" LowTemplate="{StaticResource lowTemplate}"/>
</ListBox.ItemTemplateSelector>
</ListBox>

[附](漂亮的范例):WPF中DataTemplateSelector的简单应用 - aaayi - 博客园
版权声明
本文为[阿月浑子2021]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_56814032/article/details/122092253
边栏推荐
猜你喜欢

国产首发USBhub大全,USB HUB2.0,HUB3.0,旺玖PROLIFIC、PL2586、MA8601,和芯润德SL2.2A、SL2.2S,替换汤铭的FE1.1、FE1.1S、、威锋VL810

Information encryption and information summarization

BGP 邻居

MS12_ 020 vulnerability

搭建mysql主从复制、读写分离、一主一从

PE头文件分析

Switching technology

宏晶微MS9123,USB 投屏控制芯片,USB 投屏器,支持 CVBS、S-Video 视频接口,

金融信息安全实训——22/4/18

排序方式(2)==>插入排序
随机推荐
Ruiyuan power chip, ry3715, ry3750 replacement: silijie sy7208, sy7152, Xinpeng micro ap2008 Core source mp1542, mp3213. Input voltage from 2.5V to 5.5V
蓝桥杯——十六进制与十进制之间的转换
Voisins bgp
Echars thermal map custom colors
Cs5801 specification | cs5801 HDMI to EDP conversion scheme | HDMI to DP adapter board design, hdmi2 0 turn edp1 4. Support downward compatibility
Word XML space character
蕊源电源芯片,RY3715,RY3750替换:矽力杰SY7208、SY7152、芯朋微AP2008.芯源MP1542,MP3213。2.5V至5.5V的输入电压
RTSP unauthorized access
Export excel custom style with hutool tool ----- excel compression jar
Nmap扫描和Scapy项目
Log4j Remote Code Execution Vulnerability verification
IGMP_华为
Sort method (2) = > insert sort
MS12_ 020 vulnerability
Jfinal hutool tool excel util ziputil realizes exporting excel and compressing files
NP, NSSA area
Audio power amplifier, ns4165b single channel 5.3w class AB \ D power amplifier, mainly replacing cs8571, cs8871 and cs5218 of Zhipu core and haa2018 of hailichuang
BGP 自动路由聚合
宏晶微MS9288、模拟RGB(VGA)转HDMI,HDMI发射器,音频解码器,内置MCU
2020-12-24