当前位置:网站首页>WPF DataGrid using data templates
WPF DataGrid using data templates
2022-08-10 01:28:00 【flysh05】
1. 创建数据模型类
public class TestPointModel
{
public int Id {
get; set; }
public string TestPointName {
get; set; }
public DateTime TestDateTime {
get; set; }
public string Details
{
get
{
return $"{
Id} ,{
TestPointName} have tested on {
TestDateTime}";
}
}
}
2. UI设计
<DataGrid x:Name="dgTestPoints" Margin="5" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Id}" Width="50"/>
<DataGridTextColumn Header="PointName" Binding="{Binding TestPointName}" Width="120"/>
<DataGridTextColumn Header="DateTime" Binding="{Binding TestDateTime}" Width="200"/>
</DataGrid.Columns>
<!-- Visibility 属性 Visible,Collapsed,Hidden -->
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Margin=" 10" Visibility="Visible" Text="{Binding Details}"></TextBlock>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
Create a data source using data model classes,所以AutoGenerateColumns=“False”
使用DataGridTextColumn Binding model's property fields.
3.UI 后台代码
public MainWindow()
{
InitializeComponent();
dgTestPoints.ItemsSource = CreatDataSource();
}
//Create Data sorce for DataGrid Control show
private static List<TestPointModel> CreatDataSource()
{
List<TestPointModel> testPoints = new List<TestPointModel>();
testPoints.Add(new TestPointModel() {
Id = 1, TestPointName = "VoltageTest", TestDateTime = new DateTime(2022, 7, 23) });
testPoints.Add(new TestPointModel() {
Id = 2, TestPointName = "CurrentTest", TestDateTime = new DateTime(2022, 1, 17) });
testPoints.Add(new TestPointModel() {
Id = 3, TestPointName = "SwitchTest", TestDateTime = new DateTime(2022, 9, 2) });
return testPoints;
}
Create a data collection for the data model,绑定到DataGrid 的ItemsSource 属性.
显示效果如下:
边栏推荐
猜你喜欢
selenium和驱动安装
ES6 Beginner to Mastery #15: Generator Usage
Wireshark经典实践和面试13点总结
多商户商城系统功能拆解24讲-平台端分销会员
ES6 Beginner to Mastery #13: Extension Methods for Arrays 2
Alibaba Cloud SMS Service Activation
[Cloud native] Kubernetes orchestration tools
LSTM-based distributed energy generation prediction (Matlab code implementation)
61.【快速排序法详解】
经济衰退即将来临前CIO控制成本的七种方法
随机推荐
下载markdown软件Obsidian(解决官网下载速度慢)
ES6 Beginner to Mastery #15: Generator Usage
数据库的备份与恢复「建议收藏」
【渗透工具】浏览器数据导出工具
A Shanghai technology company was fined 220,000 for brushing orders, exposing the gray industry chain of online brushing
下班后用微信处理工作时发病身亡,法院判决:工伤!
阿里云短信服务开通
LeetCode952三部曲之三:再次优化(122ms -> 96ms,超51% -> 超91%)
framework源码读后感
ES6 从入门到精通 # 12:数组的扩展方法一
【SSL集训DAY2】有趣的数【数位DP】
CAD 截断线段
【C语言】指针和数组的深入理解(第四期)
领跑政务云,连续五年中国第一
JVM内存和垃圾回收-10.直接内存
阿雷的血压有些低
redis distributed lock code example
知行合一的时候
今日睡眠质量记录61分
781. 森林中的兔子