当前位置:   article > 正文

c# wpf template itemtemplate+dataGrid

c# wpf template itemtemplate+dataGrid

1.概要

2.代码

  1. <Window x:Class="WpfApp2.Window8"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfApp2"
  7. mc:Ignorable="d"
  8. Title="Window8" Name="win" Height="450" Width="800">
  9. <Grid>
  10. <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding BookList,ElementName=win}" Grid.Row="1" Grid.Column="1">
  11. <DataGrid.Columns>
  12. <DataGridTextColumn Header="书名" Binding="{Binding Title}" />
  13. <DataGridTextColumn Header="作者" Binding="{Binding Author}" />
  14. <DataGridTemplateColumn Header="时间">
  15. <DataGridTemplateColumn.CellTemplate>
  16. <DataTemplate>
  17. <DatePicker SelectedDate="{Binding Time}" BorderThickness="0" />
  18. </DataTemplate>
  19. </DataGridTemplateColumn.CellTemplate>
  20. </DataGridTemplateColumn>
  21. </DataGrid.Columns>
  22. </DataGrid>
  23. </Grid>
  24. </Window>

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace WpfApp2
  15. {
  16. /// <summary>
  17. /// Window8.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class Window8 : Window
  20. {
  21. public Window8()
  22. {
  23. InitializeComponent();
  24. BookList.Add(new Book() { Title = "三国演义", Author = "罗贯中", Time = DateTime.Now.AddYears(-200) });
  25. BookList.Add(new Book() { Title = "红楼梦", Author = "曹雪芹", Time = DateTime.Now.AddYears(-150) });
  26. BookList.Add(new Book() { Title = "西游记", Author = "吴承恩", Time = DateTime.Now.AddYears(-230) });
  27. }
  28. public List<Book> BookList { get; set; } = new List<Book>();
  29. }
  30. }
  1. public class Book
  2. {
  3. public required string Title { get; set; }
  4. public required string Author { get; set; }
  5. public DateTime Time { get; set; }
  6. }

 

3.运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/411440
推荐阅读
相关标签
  

闽ICP备14008679号