当前位置: 首页 > news >正文

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

一、DataTemplate

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

1.代码示例

1.1 xaml.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

<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

后端不变

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

http://www.lryc.cn/news/399749.html

相关文章:

  • GuLi商城-商品服务-API-品牌管理-JSR303分组校验
  • PyTorch DataLoader 学习
  • TCP传输控制协议二
  • 【学习笔记】无人机(UAV)在3GPP系统中的增强支持(五)-同时支持无人机和eMBB用户数据传输的用例
  • 使用F1C200S从零制作掌机之debian文件系统完善NES
  • Vue 3 与 TypeScript:最佳实践详解
  • PyMysql error : Packet Sequence Number Wrong - got 1 expected 0
  • MVC 生成验证码
  • OSPF.综合实验
  • 云计算【第一阶段(29)】远程访问及控制
  • 2024前端面试真题【CSS篇】
  • python中设置代码格式,函数编写指南,类的编程风格
  • CentOS 8升级gcc版本
  • Kafka基础入门篇(深度好文)
  • C++之复合资料型态KU网址第二部V蒐NAY3989
  • 乡镇集装箱生活污水处理设备处理效率高
  • 计算机网络高频面试题
  • 进程通信(1):无名管道(pipe)
  • YOLOv10改进 | 损失函数篇 | SlideLoss、FocalLoss、VFLoss分类损失函数助力细节涨点(全网最全)
  • 【数组、特殊矩阵的压缩存储】
  • Flat Ads:金融APP海外广告投放素材的优化指南
  • DBA 数据库管理 表管理 数据批量处理。表头约束
  • C# 上位机开发之旅-委托事件的那些事[2]
  • 浏览器出现 502 Bad Gateway的原理分析以及解决方法
  • Java的高级特性
  • pip install selenium异常
  • 应急响应总结
  • 一些资源(●ˇ∀ˇ●)
  • WGCLOUD的ping设备监测可以导入excel数据吗
  • vue 画二维码及长按保存