WPF Datagrid后台合并表头并数据绑定

本文展示了如何在WPF中实现在后台合并Datagrid的表头,并进行数据绑定。通过后台代码动态添加TextBox和ComboBox,利用DataTrigger进行控件的定制。提供了UI页面的设计效果及后台实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

 UI页面

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"
                  IsReadOnly="False">
            <DataGrid.Columns>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

后台代码 

using System;
using System.Collections.Generic;
using System.Data;
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 WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private DataTable data = new DataTable();
        /// <summary>
        /// 构造方法
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            this.InitDataGrid();
            this.InitData();
        }

        /// <summary>
        /// 初始化DataGrid
        /// </summary>
        private void InitDataGrid()
        {
            DataGridTextColumn dgtx = new DataGridTextColumn();
            dgtx.Header = "第一列";
            Binding bindingF = new Binding("F");
            dgtx.Binding = bindingF;
            this.dataGrid.Columns.Add(dgtx);
            this.InitCustomHeader();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            data.Columns.Add("F");
            data.Columns.Add("A");
            data.Columns.Add("B");
            data.Columns.Add("C");

            var dr = data.NewRow();
            dr[0] = "测试1";
            dr[1] = "测试2";
            dr[2] = "测试3";
            dr[3] = "测试4";

            var dr1 = data.NewRow();
            dr1[0] = "测试1";
            dr1[1] = "测试2";
            dr1[2] = "测试3";
            dr1[3] = "测试4";

            data.Rows.Add(dr);
            data.Rows.Add(dr1);

            this.dataGrid.ItemsSource = data.DefaultView;
        }

        /// <summary>
        /// 设置标题
        /// </summary>
        private void InitCustomHeader()
        {
            DataTemplate dataTemplate = new DataTemplate();
            FrameworkElementFactory cfef = new FrameworkElementFactory(typeof(StackPanel));
            
            FrameworkElementFactory level1FEF = new FrameworkElementFactory(typeof(Label));
            level1FEF.SetValue(Label.ContentProperty, "二大列");
            level1FEF.SetValue(Label.BorderThicknessProperty, new Thickness(0, 0, 0, 0));
            level1FEF.SetValue(Label.BorderBrushProperty, new SolidColorBrush(Colors.Blue));
            level1FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level1FEF.SetValue(Label.WidthProperty, 300.00);
            cfef.AppendChild(level1FEF);

            FrameworkElementFactory level2FEF = new FrameworkElementFactory(typeof(StackPanel));
            level2FEF.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

            FrameworkElementFactory level21FEF = new FrameworkElementFactory(typeof(Label));
            level21FEF.SetValue(Label.ContentProperty, "二一列");
            level21FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level21FEF.SetValue(Label.WidthProperty, 150.00);
            level2FEF.AppendChild(level21FEF);

            FrameworkElementFactory level22FEF = new FrameworkElementFactory(typeof(Label));
            level22FEF.SetValue(Label.ContentProperty, "二二列");
            level22FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level22FEF.SetValue(Label.WidthProperty, 150.00);
            level2FEF.AppendChild(level22FEF);

            cfef.AppendChild(level2FEF);

            FrameworkElementFactory level3FEF = new FrameworkElementFactory(typeof(StackPanel));
            level3FEF.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

            FrameworkElementFactory level31FEF = new FrameworkElementFactory(typeof(Label));
            level31FEF.SetValue(Label.ContentProperty, "三一");
            level31FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level31FEF.SetValue(Label.WidthProperty, 75.00);
            level3FEF.AppendChild(level31FEF);

            FrameworkElementFactory level32FEF = new FrameworkElementFactory(typeof(Label));
            level32FEF.SetValue(Label.ContentProperty, "三二");
            level32FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level32FEF.SetValue(Label.WidthProperty, 75.00);
            level3FEF.AppendChild(level32FEF);

            FrameworkElementFactory level33FEF = new FrameworkElementFactory(typeof(Label));
            level33FEF.SetValue(Label.ContentProperty, "三三");
            level33FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level33FEF.SetValue(Label.WidthProperty, 150.00);
            level3FEF.AppendChild(level33FEF);

            cfef.AppendChild(level3FEF);

            dataTemplate.VisualTree = cfef;

            DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
            dgtc.HeaderTemplate = dataTemplate;

            this.InitCustomCell(ref dgtc);
            this.InitCustomEditCell(ref dgtc);
            this.dataGrid.Columns.Add(dgtc);

        }

        /// <summary>
        /// 设置显示单元格
        /// </summary>
        /// <param name="dgtc"></param>
        private void InitCustomCell(ref DataGridTemplateColumn dgtc)
        {
            DataTemplate dataTemplate = new DataTemplate();
            FrameworkElementFactory cfef = new FrameworkElementFactory(typeof(StackPanel));

            FrameworkElementFactory level3FEF = new FrameworkElementFactory(typeof(StackPanel));
            level3FEF.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);


            Binding bindingLevel31 = new Binding("A");
            FrameworkElementFactory level31FEF = new FrameworkElementFactory(typeof(Label));
            level31FEF.SetValue(Label.ContentProperty, bindingLevel31);
            level31FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level31FEF.SetValue(Label.WidthProperty, 75.00);
            level3FEF.AppendChild(level31FEF);

            Binding bindingLevel32 = new Binding("B");
            FrameworkElementFactory level32FEF = new FrameworkElementFactory(typeof(Label));
            level32FEF.SetValue(Label.ContentProperty, bindingLevel32);
            level32FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level32FEF.SetValue(Label.WidthProperty, 75.00);
            level3FEF.AppendChild(level32FEF);

            Binding bindingLevel33 = new Binding("C");
            FrameworkElementFactory level33FEF = new FrameworkElementFactory(typeof(Label));
            level33FEF.SetValue(Label.ContentProperty, bindingLevel33);
            level33FEF.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level33FEF.SetValue(Label.WidthProperty, 150.00);
            level3FEF.AppendChild(level33FEF);

            cfef.AppendChild(level3FEF);

            dataTemplate.VisualTree = cfef;

            dgtc.CellTemplate = dataTemplate;
        }

        /// <summary>
        /// 设置编辑单元格
        /// </summary>
        /// <param name="dgtc"></param>
        private void InitCustomEditCell(ref DataGridTemplateColumn dgtc)
        {
            DataTemplate dataTemplate = new DataTemplate();
            FrameworkElementFactory cfef = new FrameworkElementFactory(typeof(StackPanel));

            FrameworkElementFactory level3FEF = new FrameworkElementFactory(typeof(StackPanel));
            level3FEF.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);


            Binding bindingLevel31 = new Binding("A");
            FrameworkElementFactory level31FEF = new FrameworkElementFactory(typeof(TextBox));
            level31FEF.SetValue(TextBox.TextProperty, bindingLevel31);
            level31FEF.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level31FEF.SetValue(TextBox.WidthProperty, 75.00);
            level3FEF.AppendChild(level31FEF);

            Binding bindingLevel32 = new Binding("B");
            FrameworkElementFactory level32FEF = new FrameworkElementFactory(typeof(TextBox));
            level32FEF.SetValue(TextBox.TextProperty, bindingLevel32);
            level32FEF.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level32FEF.SetValue(TextBox.WidthProperty, 75.00);
            level3FEF.AppendChild(level32FEF);

            Binding bindingLevel33 = new Binding("C");
            FrameworkElementFactory level33FEF = new FrameworkElementFactory(typeof(TextBox));
            level33FEF.SetValue(TextBox.TextProperty, bindingLevel33);
            level33FEF.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            level33FEF.SetValue(TextBox.WidthProperty, 150.00);
            level3FEF.AppendChild(level33FEF);

            cfef.AppendChild(level3FEF);

            dataTemplate.VisualTree = cfef;

            dgtc.CellEditingTemplate = dataTemplate;
        }

    }
}

后台动态添加控件可参考我其他文章

WPF 后台代码动态向DataGrid添加TextBox、Combox并设置datatrigger_我的C++入门了的博客-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值