当前位置:网站首页>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 属性.
显示效果如下:
边栏推荐
猜你喜欢
随机推荐
JSP简介
YOLOV5学习笔记(七)——训练自己数据集
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
redis distributed lock code example
基于 LSTM 的分布式能源发电预测(Matlab代码实现)
ABAP中Collect的用法
KingbaseGIS Jin Cang database using manual (6.3. Geometric object creation function)
MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
【C语言】通讯录《静态内存版本》
【剑指offer】第一题 第二题
《动手学深度学习》(八) -- 多尺度标检测和单发多框检测
【SSL集训DAY3】控制棋盘【二分图匹配】
NotWritableError: The current user does not have write permissions when conda creates a new environment
YOLOV5 study notes (7) - training your own data set
十位时间戳转化成时间
arm-4-裸板开发
Eureka protects itself
In-depth understanding of multithreading (Part 1)
拼多多店铺运营不得不知的留个运营小知识