<Window x:Class="WPFTest.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:WPFTest"
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}"/>
<TextBlock Margin="10,10" Text="{Binding Name}"/>
StackPanel>
DataTemplate>
ListBox.ItemTemplate>
ListBox>
Grid>
Window>
模板是写在
和
里面的。
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;
namespace WPFTest
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//做了一个列表,变量是test,类型是Color
List test = new List();
//List的Add函数里面的参数是类类型实例
test.Add(new Color() { Code = "#E0FFFF", Name= "浅黄色" }) ;
test.Add(new Color() { Code = "#FFFACD", Name = "柠檬雪纺" });
test.Add(new Color() { Code = "#FAFAD5", Name = "柠檬雪纺" });
test.Add(new Color() { Code = "#FFEFD5", Name = "木瓜鞭" });
test.Add(new Color() { Code = "#FFE4B5", Name = "莫卡辛" });
//这里的list是ListBox空间的名称,他的资源来源于test列表
list.ItemsSource = test;
// 浅黄色 #FFFFE0 rgb(255,255,224)
//柠檬雪纺 #FFFACD RGB(255,250,205)
//浅金黄色 #FAFAD2 RGB(250,250,210)
//木瓜鞭 #FFEFD5 rgb(255,239,213)
//莫卡辛 #FFE4B5 rgb(255,228,181)
//桃扑 #FFDAB9 rgb(255,218,185)
//淡金黄色的 #EEE8AA 英镑(238,232,170)
//黄褐色 #F0E68C RGB(240,230,140)
}
}
public class Color
{
public string Code { get; set; }
public string Name { get; set; }
}
}
<Window x:Class="WPFTest.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:WPFTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="grid" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Code" Binding="{Binding Code}"/>
<DataGridTextColumn Header="Color" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border
Width="10"
Height="10"
Background="{Binding Code}"/>
<TextBlock Margin="10,0" Text="{Binding Name}"/>
<Button Content="删除"/>
<Button Content="取消"/>
StackPanel>
DataTemplate>
DataGridTemplateColumn.CellTemplate>
DataGridTemplateColumn>
DataGrid.Columns>
DataGrid>
Grid>
Window>
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;
namespace WPFTest
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//做了一个列表,变量是test,类型是Color
List test = new List();
//List的Add函数里面的参数是类类型实例
test.Add(new Color() { Code = "#E0FFFF", Name= "浅黄色" }) ;
test.Add(new Color() { Code = "#FFFACD", Name = "柠檬雪纺" });
test.Add(new Color() { Code = "#FAFAD5", Name = "柠檬雪纺" });
test.Add(new Color() { Code = "#FFEFD5", Name = "木瓜鞭" });
test.Add(new Color() { Code = "#FFE4B5", Name = "莫卡辛" });
//这里的gird是GridData空间的名称,他的资源来源于test列表
grid.ItemsSource = test;
// 浅黄色 #FFFFE0 rgb(255,255,224)
//柠檬雪纺 #FFFACD RGB(255,250,205)
//浅金黄色 #FAFAD2 RGB(250,250,210)
//木瓜鞭 #FFEFD5 rgb(255,239,213)
//莫卡辛 #FFE4B5 rgb(255,228,181)
//桃扑 #FFDAB9 rgb(255,218,185)
//淡金黄色的 #EEE8AA 英镑(238,232,170)
//黄褐色 #F0E68C RGB(240,230,140)
}
}
public class Color
{
public string Code { get; set; }
public string Name { get; set; }
}
}