c# - 垂直合并 WPF DataGrid 中的单元格

标签 c# wpf datagrid

我想在 WPF 中创建一个 DataGrid,其中一些单元格将“合并在一起”(如果它们相似的话)。

例子:

+---------+------+-----+
| Country | Name | Age |
+---------+------+-----+
|         | Lisa | 24  |
+         +------+-----+
| Danmark |  Per | 32  |
+         +------+-----+
|         | Hans | 33  |
+---------+------+-----+
| Germany | Mick | 22  |
+---------+------+-----+

有没有什么方法可以使用绑定(bind)的 DataGrid 来实现这一点?

非常感谢。

最佳答案

此类场景的技巧是使用 CollectionViewSource 中形成的 Groups 作为 DataGridItemsSource。并且,使用 DataGrid 本身作为 ColumnCellTemplate

Xaml

    <Window.Resources>
        <CollectionViewSource x:Key="CvsKey">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Country"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <Grid>
        <DataGrid x:Name="dg" Loaded="dg_Loaded" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="All" Grid.Column="0" RowHeaderWidth="0" CanUserAddRows="False" AutoGenerateColumns="False"  VerticalContentAlignment="Center" HorizontalContentAlignment="Center">            
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Country"  Width="75">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>                           
                            <Grid>
                                <TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Name"  Width="75">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DataGrid ItemsSource="{Binding Items}" IsReadOnly="True" AutoGenerateColumns="False" HeadersVisibility="None">
                                <DataGrid.Columns>
                                    <DataGridTemplateColumn Width="*">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Name}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
          </DataGrid.Columns>            
        </DataGrid>
    </Grid>
</Window>

DataGrid.Loaded 事件

 private void dg_Loaded(object sender, RoutedEventArgs e)
    {
        var groups = (this.Resources["CvsKey"] as CollectionViewSource).View.Groups;
        dg.ItemsSource = groups;
    }

这应该可以帮助您入门。

输出:

Vertical Grid Output

关于c# - 垂直合并 WPF DataGrid 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39748090/

相关文章:

C# WPF DataGrid 在列中搜索一个值,返回行索引

c# - 翻译 DataGrid MouseEvent 所以我在 View 的代码隐藏中没有代码

c# - 从 .net 程序读取 linux 中的文件?

c# - 基于 32 位或 64 位的不同结构属性

c# - Asp.Net Mvc - 如何在共享 View 中有一个 "controller"

wpf - 在数据网格的组合框列中显示图像

c# - 如何将 BorderBrush 颜色更改为默认颜色

c# - 如何隐藏/删除 WPF ColumnHeaderStyle 列标题?

c# - 无法将类型 'int' 隐式转换为 'string'

wpf - 在代码中从控件模板创建可视化树