是否可以在WPF DataGrid中获取嵌套列?

时间:2014-07-20 16:50:23

标签: c# .net wpf datagrid

是否可以在WPF DataGrid中使用嵌套列,如下图所示?

image

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

   <DataGrid ItemsSource="{Binding }" Name="dataGrid1" AutoGenerateColumns="False" 
                          ColumnHeaderHeight="50" >
                <DataGrid.Resources>

                    <Style x:Key="DataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                    <Grid>
                                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                </DataGrid.Resources>
                <DataGrid.Columns>
                        <DataGridTextColumn  Header="Main1" Width="60" 
                                            HeaderStyle="{StaticResource DataGridColumnHeader}">
                        <DataGridTextColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock   Width="60"/>

                                    <DataGridColumnHeader Content="Nested1" Width="60"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTextColumn.HeaderTemplate>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="Nested2" Width="60"
                                             HeaderStyle="{StaticResource DataGridColumnHeader}">
                        <DataGridTextColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock FontWeight="Bold" Text="Main1"/>

                                    <DataGridColumnHeader Content="Nested2" Width="60" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTextColumn.HeaderTemplate>
                    </DataGridTextColumn>
                    <DataGridTextColumn Width="60"
                                             HeaderStyle="{StaticResource DataGridColumnHeader}">
                        <DataGridTextColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock />

                                    <DataGridColumnHeader Content="Nested3" Width="60"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTextColumn.HeaderTemplate>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>

答案 1 :(得分:0)

这是Hub of Computer Tricks and Programming的解决方案。该解决方案允许您调整嵌套列的大小。

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="700">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Width="{Binding Path=Width, ElementName=datagrid1}" HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column2, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column3, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column4, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column5, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column6, Path=ActualWidth}"/>
                <ColumnDefinition Width="{Binding ElementName=Column7, Path=ActualWidth}"/>
            </Grid.ColumnDefinitions>
            <Border Grid.Column="1" Grid.ColumnSpan="2" BorderBrush="Gray" HorizontalAlignment="Stretch" BorderThickness="1">
                <Label Name='Name' HorizontalContentAlignment='Stretch'>Name</Label>
            </Border>
            <Border Grid.Column="3" Grid.ColumnSpan="4" BorderBrush="Gray" HorizontalAlignment="Stretch" BorderThickness="1">
                <Label Name='Address' HorizontalContentAlignment='Stretch'>Address</Label>
            </Border>
        </Grid>
        <DataGrid Name="datagrid1" Grid.Row="1" HorizontalAlignment="Left">
            <DataGrid.Columns>
                <DataGridTextColumn Header="S.No" MinWidth="80" x:Name="Column1"/>
                <DataGridTextColumn Header="First Name" MinWidth="100" x:Name="Column2"/>
                <DataGridTextColumn Header="Last Name" MinWidth="100" x:Name="Column3"/>
                <DataGridTextColumn Header="Address 1" MinWidth="90" x:Name="Column4"/>
                <DataGridTextColumn Header="Address 2" MinWidth="90" x:Name="Column5"/>
                <DataGridTextColumn Header="City" MinWidth="90" x:Name="Column6"/>
                <DataGridTextColumn Header="State" MinWidth="90" x:Name="Column7"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>