当前位置:网站首页>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 属性。
显示效果如下:
边栏推荐
猜你喜欢
随机推荐
Wireshark classic practice and interview 13-point summary
A Shanghai technology company was fined 220,000 for brushing orders, exposing the gray industry chain of online brushing
Linux安装Oracle和postgrepSQL数据库
下班后用微信处理工作时发病身亡,法院判决:工伤!
【C语言】指针和数组的深入理解(第四期)
ES6 从入门到精通 # 14:迭代器 Iterator 的用法
[C language] In-depth understanding of pointers and arrays (issue 4)
《GB5084-2021》PDF下载
【哲理】读书的意义
CAD 截断线段
直播app开发搭建,flutter 实现自适应、自动换行、相对布局
Gartner's global integrated system market data tracking, hyperconverged market growth rate is the first
Gumbel distribution of discrete choice model
【集训DAY4】矩形【线段树】
NTU General Database-Gbase-8a-Learning-04-Deploying Distributed Clusters
nfs配置
Creo5.0入门教程赠素材
LeetCode952三部曲之三:再次优化(122ms -> 96ms,超51% -> 超91%)
新开窗口 展示协议
Description of AirFlow