WPF的DataGrid表格动态加载合并列

效果

 

xaml

 <DataGridTemplateColumn >
                                <DataGridTemplateColumn.HeaderTemplate>
                                    <DataTemplate >
                                        <StackPanel HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
                                            <Border>
                                                <Label Content="行度" Width="240" FontSize="14" Foreground="#969696"  HorizontalContentAlignment="Center"/>
                                            </Border>
                                            <Border>
                                                <GridSplitter BorderBrush="#DCDCDC"   BorderThickness="0.5"  HorizontalAlignment="Stretch"/>
                                            </Border>
                                            <Border>
                                                <StackPanel Width="240" Orientation="Horizontal">
                                                    <Label Content="上期" Width="120" FontSize="14" Foreground="#969696" HorizontalContentAlignment="Center" HorizontalAlignment="Center"  VerticalContentAlignment="Center"/>
                                                    <GridSplitter BorderBrush="#DCDCDC"  BorderThickness="0.5"/>
                                                    <Label Content="本期" Width="120" FontSize="14" Foreground="#969696"  HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center"/>
                                                </StackPanel>
                                            </Border>
                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Width="Auto" Height="Auto" Orientation="Horizontal">
                                            <Border>
                                                <Label   Width="120" Content="{Binding Code, Mode=TwoWay}" HorizontalAlignment="Center"  VerticalAlignment="Center" HorizontalContentAlignment="Center"/>
                                            </Border>
                                            <Border>
                                                <GridSplitter  Background="Black" Height="32" BorderThickness="0.5" BorderBrush="#DCDCDC"/>
                                            </Border>
                                            <Border>
                                                <Label   Width="120" Content="{Binding CommunityName, Mode=TwoWay}"  HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center"/>
                                            </Border>
                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

后台代码加载

 private void AddCells()
        {          
            var stackPanelFirst = new FrameworkElementFactory(typeof(StackPanel));
            stackPanelFirst.SetValue(StackPanel.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
            stackPanelFirst.SetValue(StackPanel.VerticalAlignmentProperty, VerticalAlignment.Center);

            var labelhangdu = new FrameworkElementFactory(typeof(Label));
            labelhangdu.SetValue(Label.ContentProperty, "行度");
            labelhangdu.SetValue(Label.ForegroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#969696")));
            labelhangdu.SetValue(Label.WidthProperty, (double)240);
            labelhangdu.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);

            var borderA = new FrameworkElementFactory(typeof(Border));
            borderA.SetValue(Border.BorderThicknessProperty, new Thickness(0.5));
            borderA.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            borderA.SetValue(BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            borderA.AppendChild(labelhangdu);
            stackPanelFirst.AppendChild(borderA);


            var BorderG = new FrameworkElementFactory(typeof(Border));
            BorderG.SetValue(Border.BorderThicknessProperty, new Thickness(0.5));
            BorderG.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            BorderG.SetValue(BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            var GridSplitterA = new FrameworkElementFactory(typeof(GridSplitter));
            GridSplitterA.SetValue(GridSplitter.BorderThicknessProperty, new Thickness(0.5));
            GridSplitterA.SetValue(GridSplitter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
         
            GridSplitterA.SetValue(GridSplitter.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            BorderG.AppendChild(GridSplitterA);
            stackPanelFirst.AppendChild(BorderG);

            var stackPanelB = new FrameworkElementFactory(typeof(StackPanel));
            stackPanelB.SetValue(StackPanel.WidthProperty, (double)240);
            stackPanelB.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

            var labelShangqi = new FrameworkElementFactory(typeof(Label));
            labelShangqi.SetValue(Label.ContentProperty, "上期");
            labelShangqi.SetValue(Label.WidthProperty, (double)120);
            labelShangqi.SetValue(Label.ForegroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#969696")));
            labelShangqi.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            labelShangqi.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Center);
            labelShangqi.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Center);

            var GridSplitterB = new FrameworkElementFactory(typeof(GridSplitter));
            GridSplitterB.SetValue(GridSplitter.BorderThicknessProperty, new Thickness(0.5));
            GridSplitterB.SetValue(GridSplitter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            GridSplitterB.SetValue(GridSplitter.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            GridSplitterB.SetValue(GridSplitter.BorderThicknessProperty, new Thickness(0.5));

            var labelXiaQi = new FrameworkElementFactory(typeof(Label));
            labelXiaQi.SetValue(Label.ContentProperty, "下期");
            labelXiaQi.SetValue(Label.WidthProperty, (double)120);
            labelXiaQi.SetValue(Label.ForegroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#969696")));
            labelXiaQi.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            labelXiaQi.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Center);
            labelXiaQi.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Center);

            stackPanelB.AppendChild(labelShangqi);
            stackPanelB.AppendChild(GridSplitterB);
            stackPanelB.AppendChild(labelXiaQi);

            var BorderC = new FrameworkElementFactory(typeof(Border));
            //BorderC.SetValue(Border.BorderThicknessProperty, new Thickness(0.5));
            //BorderC.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            //BorderC.SetValue(BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            BorderC.AppendChild(stackPanelB);
            stackPanelFirst.AppendChild(BorderC);

            var dataTemplate = new DataTemplate
            {
                VisualTree = stackPanelFirst
            };



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

            var BorderCellsA = new FrameworkElementFactory(typeof(Border));
            var BorderCellsB = new FrameworkElementFactory(typeof(Border));
            var BorderCellsC = new FrameworkElementFactory(typeof(Border));


            var textBlockA = new FrameworkElementFactory(typeof(Label));
            textBlockA.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Center);
            textBlockA.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            textBlockA.SetValue(Label.WidthProperty, (double)120);
            textBlockA.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);

            textBlockA.SetBinding(Label.ContentProperty, new Binding() { Path =new PropertyPath("CommunityName"), Mode = BindingMode.TwoWay });

            BorderCellsA.AppendChild(textBlockA);

            var GridSplitterCells = new FrameworkElementFactory(typeof(GridSplitter));
            GridSplitterCells.SetValue(GridSplitter.BorderThicknessProperty, new Thickness(0.5));
            GridSplitterCells.SetValue(GridSplitter.HeightProperty, (double)100);
            GridSplitterCells.SetValue(GridSplitter.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCDCDC")));
            BorderCellsB.AppendChild(GridSplitterCells);

            var textBlockC = new FrameworkElementFactory(typeof(Label));
            textBlockC.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Center);
            textBlockC.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            textBlockC.SetValue(Label.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            textBlockC.SetValue(Label.WidthProperty, (double)120);

            textBlockC.SetBinding(Label.ContentProperty, new Binding() { Path = new PropertyPath("Code"), Mode = BindingMode.TwoWay });

            BorderCellsC.AppendChild(textBlockC);

            stackPanelCells.AppendChild(BorderCellsA);
            stackPanelCells.AppendChild(BorderCellsB);
            stackPanelCells.AppendChild(BorderCellsC);

            var dataTemplateCells = new DataTemplate
            {
                VisualTree = stackPanelCells
            };

            var templateColumn = new DataGridTemplateColumn
            {
                HeaderTemplate = dataTemplate,
                CellTemplate = dataTemplateCells
            };

            DataGrid_MeterReading.Columns.Add(templateColumn);
        }
原文地址:https://www.cnblogs.com/CityOfThousandFires/p/13471607.html