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

一、DataTemplate

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

1.代码示例

1.1 xaml.cs

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace 学习
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Color> test = new List<Color>();

            test.Add(new Color() { Code = "Red", Name = "红色" });
            test.Add(new Color() { Code = "BLUE", Name = "蓝色" });
            test.Add(new Color() { Code = "YELLOW", Name = "黄色" });
            test.Add(new Color() { Code = "GREEN", Name = "绿色" });

            list.ItemsSource = test;
        }
    }

    public class Color
    {
        public string Code { get; set; }

        public string Name { get; set; }
    }
}

1.2 xaml

XML 复制代码
<Window x:Class="学习.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:学习"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <ListBox x:Name="list">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border Width="10" 
                                Height="10"
                                Background="{Binding Code}">
                        </Border>
                        <TextBlock Margin="10,0" 
                                   Text="{Binding Name}">
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

2.代码结果

3.代码示例2

后端不变

XML 复制代码
<Window x:Class="学习.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:学习"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <DataGrid
            x:Name="list"
            AutoGenerateColumns="False"
            CanUserAddRows="False">
            <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding Code}" Header="Code"/>

                <DataGridTemplateColumn Header="操作"><!--可操作的-->
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Border Width="10"
                                        Height="10"
                                        Background="{Binding Code}">
                                </Border>
                                <TextBlock Margin="10" Text="{Binding Name}">
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

4.代码结果

相关推荐
map_vis_3d44 分钟前
JSAPIThree 加载简单点图层学习笔记:SimplePoint 散点可视化
笔记·学习·信息可视化·mapvthree·jsapithree·simplepoint·点图层
西瓜堆6 小时前
提示词工程学习笔记: 工程技术行业提示词推荐
笔记·学习
阿蒙Amon7 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
charlie1145141919 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式
好奇龙猫9 小时前
【AI学习-comfyUI学习-三十二节-FLXU原生态反推+controlnet depth(UNion)工作流-各个部分学习】
人工智能·学习
i橡皮擦9 小时前
TheIsle恐龙岛读取游戏基址做插件(C#语言)
开发语言·游戏·c#·恐龙岛·theisle
好奇龙猫10 小时前
【大学院-筆記試験練習:数据库(データベース問題訓練) と 软件工程(ソフトウェア)(7)】
学习
j_jiajia10 小时前
(一)人工智能算法之监督学习——KNN
人工智能·学习·算法
2301_7833601311 小时前
关于RNAseq——从fastq到gene_counts全流程
笔记·学习
_李小白11 小时前
【AlohaMini学习笔记】第三天:AlohaMini相关技术
笔记·学习