【问题标题】:Disable collapsibility of DataGrid group headers禁用 DataGrid 组标题的可折叠性
【发布时间】:2020-03-09 14:03:11
【问题描述】:

我有一个在 xaml 中定义了 DataGrid 的 UWP 应用程序。

DataGrid 包含分组数据。当它显示时,组标题左侧有一个向下的克拉,可以单击,导致该组折叠。据我所知,这是我们没有特别要求的默认行为。

我的客户不喜欢这个并希望我删除该功能。我该怎么做?

【问题讨论】:

    标签: wpf xaml uwp datagrid


    【解决方案1】:

    您可以通过修改DataGridRowGroupHeader.HeaderStyle 来禁用该功能。简单的方法是将IsHitTestVisible 添加到False。这将禁用扩展器,因此组不会折叠。

    <controls:DataGrid>
       <controls:DataGrid.RowGroupHeaderStyles>
         <Style TargetType="controls:DataGridRowGroupHeader">     
            <Setter Property="IsHitTestVisible" Value="False"/>
         </Style>
       </controls:DataGrid.RowGroupHeaderStyles>
    </controls:DataGrid>
    

    编辑

    虽然没有消除指标。

    这是删除指示器的新样式

    <controls:DataGrid>
        <controls:DataGrid.RowGroupHeaderStyles>
            <Style TargetType="controls:DataGridRowGroupHeader">
                <Setter Property="IsTabStop" Value="False"/>
                <Setter Property="IsHitTestVisible" Value="False"/>
                <Setter Property="FontSize" Value="15"/>
                <Setter Property="MinHeight" Value="32"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:DataGridRowGroupHeader">
                            <Grid x:Name="RowGroupHeaderRoot" MinHeight="{TemplateBinding MinHeight}">
    
    
                                <Grid.Resources>
                                    <ControlTemplate x:Key="ToggleButtonTemplate" TargetType="ToggleButton">
                                        <Grid Background="{TemplateBinding Background}">
    
    
                                        </Grid>
                                    </ControlTemplate>
                                </Grid.Resources>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
    
                                <Rectangle x:Name="IndentSpacer" Grid.Column="1"/>
                                <ToggleButton x:Name="ExpanderButton" Grid.Column="2" Height="12" Width="12" Template="{StaticResource ToggleButtonTemplate}"
                                    IsTabStop="False" Margin="12,0,0,0" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}"/>
    
                                <StackPanel Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Center" Margin="12,0,0,0">
                                    <TextBlock x:Name="PropertyNameElement" Margin="4,0,0,0" Visibility="{TemplateBinding PropertyNameVisibility}" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
                                    <TextBlock x:Name="PropertyValueElement" Margin="4,0,0,0" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
                                    <TextBlock x:Name="ItemCountElement" Margin="4,0,0,0" Visibility="{TemplateBinding ItemCountVisibility}" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{TemplateBinding Foreground}"/>
                                </StackPanel>
    
                                <Rectangle x:Name="CurrencyVisual" Grid.ColumnSpan="5"
                                    StrokeThickness="1" Fill="Transparent"
                                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False" Opacity="0"/>
                                <Grid x:Name="FocusVisual" Grid.ColumnSpan="5" IsHitTestVisible="False" Opacity="0">
                                    <Rectangle  StrokeThickness="2" Fill="Transparent"
                                               HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False"/>
                                    <Rectangle  StrokeThickness="1" Fill="Transparent"
                                               HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False" Margin="2"/>
                                </Grid>
    
    
                                <Rectangle x:Name="BottomGridLine" Grid.ColumnSpan="5" Height="1" Grid.Row="1"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </controls:DataGrid.RowGroupHeaderStyles>
    </controls:DataGrid>
    

    【讨论】:

    • 谢谢。这确实可以防止组崩溃,这很好,尽管它不会消除指标。
    • 好的,我也会添加的
    • 我已根据您的要求进行了修改。请验证相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多