WPF数据模板用法

 优点:方便维护,根据数据产生ui

 

Xmal:

<ListBox x:Name="list">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Border Width="10" Height="10" Margin="5" Background="{Binding name}"/>
                    <TextBlock Text="{Binding text}" Margin="15"  />
                </StackPanel>
            </DataTemplate>

        </ListBox.ItemTemplate>
    </ListBox>

Xmal.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<color> ColorList = new List<color>();
            ColorList.Add(new color() { name="#deab8a", text="赤白橡" });
            ColorList.Add(new color() { name = "#fedcbd", text = "肌色" });
            ColorList.Add(new color() { name = "#f47920", text = "橙色" });
            ColorList.Add(new color() { name = "#905a3d", text = "灰茶" });
            list.ItemsSource = ColorList; 
        }
        
    }
    public class color
    {
        public string name { get; set; }
        public string text { get; set; }
    } 

运行效果:

DataGrid使用数据模板

xmal

 <DataGrid x:Name="list" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding name}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding text}" Header="Text"/>
            <DataGridTemplateColumn Header="操作">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Content="删除"/>
                            <Button Content="复制"/>
                            <Button Content="保存"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

xmal.cs

/// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<color> ColorList = new List<color>();
            ColorList.Add(new color() { name="#deab8a", text="赤白橡" });
            ColorList.Add(new color() { name = "#fedcbd", text = "肌色" });
            ColorList.Add(new color() { name = "#f47920", text = "橙色" });
            ColorList.Add(new color() { name = "#905a3d", text = "灰茶" });
            list.ItemsSource = ColorList; 
        }
        
    }
    public class color
    {
        public string name { get; set; }
        public string text { get; set; }
    } 

运行效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值