WPF DataGrid GroupStyle

后端 未结 1 1796
无人共我
无人共我 2021-02-09 14:13

I have the following DataGrid in WPF with two groups.
First group is a bool flag which represents if a person is active/inactive.
The second group (or sub-g

相关标签:
1条回答
  • 2021-02-09 15:07

    I would highly recommend changing the data structure to:

    public class Person {
        public bool Active { get; set; }
        public int ID { get; set; }
        public string Name { get; set; }
    
        public Collection Cities { get; set; }
    }
    

    Otherwise you can change this GroupStyle

    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Background="LightSteelBlue">
                    <TextBlock Text="{Binding Name}" Foreground="White" Margin="5 2 5 2"/>
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
    

    To

    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="EditButtonColumn" />
                            <ColumnDefinition SharedSizeGroup="IDColumn"  />
                            <ColumnDefinition SharedSizeGroup="NameColumn" />
                            <ColumnDefinition SharedSizeGroup="PresenterColumn" Width="*" />
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" BorderThickness="0" Content="Edit" Margin="3"
                    CommandParameter="{Binding Path=Items[0]}" />
                        <TextBlock Grid.Column="1" Text="{Binding Path=Items[0].ID}" />
                        <TextBlock Grid.Column="2" Text="{Binding Path=Items[0].Name}" />
    
                        <ItemsPresenter Grid.Column="3" />
                    </Grid>
                    </ControlTemplate>
                </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
    

    Change the template to suit your needs

    0 讨论(0)
提交回复
热议问题