微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在具有多个级别的WPF数据网格中显示分组?

我对 WPF比较陌生,所以我理解Styles和setters,但我在这个问题上遇到了麻烦.

我正在使用WPF数据网格,需要显示多个级别的分组.我希望第二和第三组级别比顶级更加缩进.

下面的代码显示组级别,但它会将它们一个显示在另一个上面,并使它们成为嵌套组级别无法分辨的事实.

<Style x:Key="GroupHeaderStyle" targettype="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate targettype="{x:Type GroupItem}">
                    <Expander IsExpanded="True">
                        <Expander.Header>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如何根据级别将组头缩进?

解决方法

如果来自未来的任何人遇到这个.我能够嵌套组并设置它们的样式,这样它们就不会通过执行以下操作而叠加在一起.

属性添加到CollectionViewSource:

<Window.Resources>
    <CollectionViewSource x:Key="cvs" Source="{Binding TestData}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="TopProperty"/>
            <PropertyGroupDescription PropertyName="SubProperty"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

然后在DataGrid XAML中,您必须指定2个GroupStyles,第二个将用于嵌套组.我将边距添加到第二组的StackPanel中,将文本推向右侧,使其看起来像是在相应的列中.

<DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style targettype="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate targettype="{x:Type GroupItem}">
                                    <StackPanel>
                                        <Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,1" Margin="5,0">                                            
                                            <StackPanel   Height="23" Orientation="Horizontal" Margin="3,0" Background="#FFE6E6E6">
                                                <TextBlock FontWeight="Bold"  Text="{Binding Path=Name}" Margin="5,0" Width="100" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </Border>
                                            <ItemsPresenter />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,0">
                                <StackPanel   Height="23" Orientation="Horizontal" Margin="3,0" Background="#FFF3F3F3">
                                    <TextBlock FontWeight="Bold"  Text="{Binding Path=Name}" Margin="55,0" Width="100" VerticalAlignment="Center"/>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
</DataGrid.GroupStyle>

我能在这里找到更多:
https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐