当前位置:网站首页>WPF DataGrid 使用数据模板
WPF DataGrid 使用数据模板
2022-08-09 23:13: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>
使用数据模型类创建数据源,所以AutoGenerateColumns=“False”
使用DataGridTextColumn 绑定模型的属性字段。
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;
}
创建数据模型的数据集合,绑定到DataGrid 的ItemsSource 属性。
显示效果如下:


边栏推荐
猜你喜欢

ES6 从入门到精通 # 13:数组的扩展方法二

Has your phone ever been monitored?
![[C language] In-depth understanding of pointers and arrays (issue 4)](/img/22/ad31b47bb8cc1722121959efddefd5.png)
[C language] In-depth understanding of pointers and arrays (issue 4)

In-depth understanding of multithreading (Part 1)

从TRPO到PPO(理论分析与数学证明)

工程 (七) ——PolarSeg点云语义分割

ES6 从入门到精通 # 14:迭代器 Iterator 的用法

RebatMq消息中间件(一) 各个中间件介绍

【渗透工具】浏览器数据导出工具

数字孪生电力系统,可视化应用实现科学调度的电子设备
随机推荐
RebatMq消息中间件(一) 各个中间件介绍
Description of AirFlow
用函数统计最长单词的字母数量
Golden Warehouse Database KingbaseGIS User Manual (6.5. Geometry Object Editing Function)
【数据存储】signed,unsigned到底怎么区分?如何计算?
下载markdown软件Obsidian(解决官网下载速度慢)
【集训DAY3】阶乘【数学】
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
Eureka自我保护
如何抑制告警风暴?
《动手学深度学习》(八) -- 多尺度标检测和单发多框检测
博弈小游戏
Gartner's global integrated system market data tracking, hyperconverged market growth rate is the first
HStreamDB v0.9 发布:分区模型扩展,支持与外部系统集成
领跑政务云,连续五年中国第一
从TRPO到PPO(理论分析与数学证明)
【SSL集训DAY2】Sequence【数学】
什么是服务治理
CAD 绘制圆角处理
信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念