当前位置:   article > 正文

WPF学习(4) -- 数据模板

WPF学习(4) -- 数据模板

一、DataTemplate

在WPF(Windows Presentation Foundation)中,DataTemplate 用于定义数据的可视化呈现方式。它允许你自定义如何展示数据对象,从而实现更灵活和丰富的用户界面。DataTemplate 通常用于控件(如ListBoxComboBoxDataGrid等)的项模板。

1.代码示例

1.1 xaml.cs

  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.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Collections.ObjectModel;
  16. namespace 学习
  17. {
  18. public partial class MainWindow : Window
  19. {
  20. public MainWindow()
  21. {
  22. InitializeComponent();
  23. List<Color> test = new List<Color>();
  24. test.Add(new Color() { Code = "Red", Name = "红色" });
  25. test.Add(new Color() { Code = "BLUE", Name = "蓝色" });
  26. test.Add(new Color() { Code = "YELLOW", Name = "黄色" });
  27. test.Add(new Color() { Code = "GREEN", Name = "绿色" });
  28. list.ItemsSource = test;
  29. }
  30. }
  31. public class Color
  32. {
  33. public string Code { get; set; }
  34. public string Name { get; set; }
  35. }
  36. }

1.2 xaml

  1. <Window x:Class="学习.MainWindow"
  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:学习"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <ListBox x:Name="list">
  11. <ListBox.ItemTemplate>
  12. <DataTemplate>
  13. <StackPanel Orientation="Horizontal">
  14. <Border Width="10"
  15. Height="10"
  16. Background="{Binding Code}">
  17. </Border>
  18. <TextBlock Margin="10,0"
  19. Text="{Binding Name}">
  20. </TextBlock>
  21. </StackPanel>
  22. </DataTemplate>
  23. </ListBox.ItemTemplate>
  24. </ListBox>
  25. </Grid>
  26. </Window>

2.代码结果

3.代码示例2

后端不变

  1. <Window x:Class="学习.MainWindow"
  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:学习"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <DataGrid
  11. x:Name="list"
  12. AutoGenerateColumns="False"
  13. CanUserAddRows="False">
  14. <DataGrid.Columns>
  15. <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
  16. <DataGridTextColumn Binding="{Binding Code}" Header="Code"/>
  17. <DataGridTemplateColumn Header="操作"><!--可操作的-->
  18. <DataGridTemplateColumn.CellTemplate>
  19. <DataTemplate>
  20. <StackPanel Orientation="Horizontal">
  21. <Border Width="10"
  22. Height="10"
  23. Background="{Binding Code}">
  24. </Border>
  25. <TextBlock Margin="10" Text="{Binding Name}">
  26. </TextBlock>
  27. </StackPanel>
  28. </DataTemplate>
  29. </DataGridTemplateColumn.CellTemplate>
  30. </DataGridTemplateColumn>
  31. </DataGrid.Columns>
  32. </DataGrid>
  33. </Grid>
  34. </Window>

4.代码结果

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

闽ICP备14008679号