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.代码结果

相关推荐
我先去打把游戏先12 小时前
ESP32学习笔记(基于IDF):SmartConfig一键配网
笔记·嵌入式硬件·mcu·物联网·学习·esp32·硬件工程
YuanlongWang16 小时前
C# 基础——装箱和拆箱
java·开发语言·c#
MicroTech202518 小时前
微算法科技(MLGO)研发突破性低复杂度CFG算法,成功缓解边缘分裂学习中的掉队者问题
科技·学习·算法
future141220 小时前
MCU硬件学习
单片机·嵌入式硬件·学习
好奇龙猫20 小时前
日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(4):文法運用
学习
mtactor20 小时前
投资理财学习笔记
笔记·学习·金融
浮游本尊21 小时前
React 18.x 学习计划 - 第四天:React Hooks深入
前端·学习·react.js
QQ129584550421 小时前
C# 如何能够创建一个MVC的WEB项目
c#·mvc
立志成为大牛的小牛1 天前
数据结构——二十六、邻接表(王道408)
开发语言·数据结构·c++·学习·程序人生
Olrookie1 天前
若依前后端分离版学习笔记(二十)——实现滑块验证码(vue3)
java·前端·笔记·后端·学习·vue·ruoyi