WPF DataGrid 使用数据模板(2)

效果如下:

1. UI XAML设计
<DataGrid x:Name="gdTestPoints" AutoGenerateColumns="False" Margin="5">
   <DataGrid.Columns>
     <DataGridTextColumn Header="TestPointName" Binding="{Binding TestPointName}" Width="120"/>
     <DataGridTextColumn Header="TestStartDate" Binding="{Binding TestDate}" Width="200"/>
   </DataGrid.Columns>


       <DataGrid.RowDetailsTemplate>
          <DataTemplate>
             <DockPanel Background="GhostWhite">
                <Image DockPanel.Dock="Left" Source="{Binding ImageUrl}" Height="24" Margin="10"/>

                  <Grid Margin="0 10">
                      <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="Auto"/>
                          <ColumnDefinition Width="\*"/>
                       </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                         </Grid.RowDefinitions>

                 <TextBlock Text="ID:" FontWeight="Bold"/>
                 <TextBlock Text="{Binding Id}" Grid.Column="1"/>
                 <TextBlock Text="TestPointName:" FontWeight="Bold" Grid.Row="1"/>
                 <TextBlock Text="{Binding TestPointName}" Grid.Column="1" Grid.Row="1"/>
                  <TextBlock Text="TestStartDate:" FontWeight="Bold" Grid.Row="2"/>
                  <TextBlock Text="{Binding TestDate}" Grid.Column="1" Grid.Row="2"/>
                </Grid>
            </DockPanel>
         </DataTemplate>
     </DataGrid.RowDetailsTemplate>
   </DataGrid>
2. UI 后台代码
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>

public partial class MainWindow : Window
{
       public class TestPoint
        {
            public int Id { get; set; }
            public string TestPointName { get; set; }
            public DateTime TestDate { get; set; }
            public string ImageUrl { get; set; }
        }

        public MainWindow()
        {
            InitializeComponent();
            List<TestPoint> points = new List<TestPoint>();
            //ImageUrl = "http://www.wpf-tutorial.com/images/misc/john\_doe.jpg"
            points.Add(new TestPoint() { Id = 1, TestPointName = "LCD Test", TestDate = new DateTime(2022, 7, 23), ImageUrl = "yes.png" });
            points.Add(new TestPoint() { Id = 2, TestPointName = "Temperateure Test", TestDate = new DateTime(2022, 1, 17) });
            points.Add(new TestPoint() { Id = 3, TestPointName = "Voltage Test", TestDate = new DateTime(2022, 9, 2), ImageUrl = "No.png" });
            gdTestPoints.ItemsSource = points ;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值