wpf: 数据模板


控件操作的反面例子如下:

数据和UI混在一起。i是数据,却和UI操作写成一堆。

 <ListBox x:Name="lbx1"/>
            for(int i=0;i<10;i++)
            {
                lbx1.Items.Add(new ListBoxItem()
                {
                    Content = new TextBlock()
                    {
                        Text = i.ToString()
                    }
                });
            }


实现数据驱动方式操作控件 

        <ListBox x:Name="lbx1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Border Width="10" Height="10" Background="{Binding colorName}"/>
                        <TextBlock Margin="10,0" Text="{Binding disName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
           
        </ListBox>
            var list1 = new List<MyColor>();
            list1.Add(new MyColor() { colorName = "#FF0000", disName = "Red" });
            list1.Add(new MyColor() { colorName = "#00FF00", disName = "Green" });
            list1.Add(new MyColor() { colorName = "#0000FF", disName = "Blue" });
            lbx1.ItemsSource = list1;

image.png


数据模板,表格控件的例子

image.png

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="grid" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Code}"/>
                <DataGridTextColumn Binding="{Binding Name}"/>
                <DataGridTemplateColumn Header="操作">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Border Width="10" Height="10" Background="{Binding Code}"/>
                                <TextBlock Margin="10,0" Text="{Binding Name}"/>
                            </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 WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var list1 = new List<MyColor>();
            list1.Add(new MyColor() { Code = "#FF0000", Name = "Red" });
            list1.Add(new MyColor() { Code = "#00FF00", Name = "Green" });
            list1.Add(new MyColor() { Code = "#0000FF", Name = "Blue" });
            grid.ItemsSource = list1;
        }
    }

    public class MyColor
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
}

上面例子如果想提供  添加,删除,复制按钮,只需要改下数据模板即可。

注意数据模板中只允许放一个元素,所以你得用容器放入多个元素。

  <DataTemplate>
                            <StackPanel>
                                <Border Width="10" Height="10" Background="{Binding Code}"/>
                                <TextBlock Margin="10,0" Text="{Binding Name}"/>
                                <Button Content="复制"/>
                                <Button Content="删除"/>
                                <Button Content="添加"/>
                            </StackPanel>
                        </DataTemplate>

image.png




本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2024-06-09 22:25:08 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864