在WPF(Windows Presentation Foundation)中,DataTemplate 用于定义数据的可视化呈现方式。它允许你自定义如何展示数据对象,从而实现更灵活和丰富的用户界面。DataTemplate 通常用于控件(如ListBox、ComboBox、DataGrid等)的项模板。
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 test = new List(); 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; } } } 
后端不变
