扩展行详细信息后,WPF DataGrid行未调整高度

时间:2011-09-12 20:31:18

标签: wpf datagrid height

我有一个带有分层数据的DataGrid。每当我折叠我的行详细信息时,父行不会自动调整大小。这最终意味着随着我不断扩展我的行详细信息,父网格继续占用越来越多的空间,直到我在同一级别折叠一行。我窥探了应用程序,发现保持新高度的最低级别元素是DataGridRowsPresenter,它是PART_ColumnHeadersPresenter。

我有自己的行和网格ControlTemplate:

<Style x:Key="{x:Type Custom:DataGrid}" TargetType="{x:Type Custom:DataGrid}">
        <Setter Property="CanUserResizeRows" Value="False" />
        <Setter Property="Background"
            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FF688CAF" />
        <Setter Property="BorderThickness" Value="1" />
        <!-- This is needed to force DG to have a non-default value.  Otherwise the DGR.DetailsVisibility cannot have a value of VisibleWhenSelected by default. -->
        <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
        <Setter Property="ScrollViewer.CanContentScroll"
            Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Custom:DataGrid}">
                    <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True"
                        Padding="{TemplateBinding Padding}">
                        <ScrollViewer Focusable="false"
                                  Name="DG_ScrollViewer">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>

                                        <!--Left Column Header Corner -->
                                        <Button Command="{x:Static Custom:DataGrid.SelectAllCommand}"
                                            Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Custom:DataGrid}}, Path=CellsPanelHorizontalOffset}"
                                            Template="{StaticResource SelectAllButtonTemplate}"
                                            Focusable="false"
                                            Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Custom:DataGrid}}, Path=HeadersVisibility, Converter={x:Static Custom:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static Custom:DataGridHeadersVisibility.All}}" />

                                        <!--Column Headers-->
                                        <Custom:DataGridColumnHeadersPresenter Grid.Column="1" 
                                                   Name="PART_ColumnHeadersPresenter"
                                                   Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Custom:DataGrid}}, Path=HeadersVisibility, Converter={x:Static Custom:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static Custom:DataGridHeadersVisibility.Column}}"/>

                                        <!--DataGrid content-->
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" 
                                                                Grid.Row="1" 
                                                                Grid.ColumnSpan="2" 
                                                                CanContentScroll="{TemplateBinding CanContentScroll}"
                                                                Height="Auto" VerticalAlignment="Stretch">


                                        </ScrollContentPresenter>

                                        <ScrollBar Grid.Row="1" Grid.Column="2" Name="PART_VerticalScrollBar"
                                         Orientation="Vertical"
                                         Maximum="{TemplateBinding ScrollableHeight}"
                                         ViewportSize="{TemplateBinding ViewportHeight}"
                                         Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                                        <Grid Grid.Row="2" Grid.Column="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Custom:DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar Grid.Column="1"
                                                   Name="PART_HorizontalScrollBar"
                                                   Orientation="Horizontal"
                                                   Maximum="{TemplateBinding ScrollableWidth}"
                                                   ViewportSize="{TemplateBinding ViewportWidth}"
                                                   Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                                   Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
        </Style.Triggers>
    </Style>

<ControlTemplate x:Key="PlainDataGridRowControlTemplate" TargetType="{x:Type Custom:DataGridRow}">
        <Grid x:Name="LayoutRoot">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid x:Name="DGR_Border" 
                Background="Transparent"
                SnapsToDevicePixels="True">
                <Custom:SelectiveScrollingGrid Name="DGR_SelectiveScrollingGrid">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Custom:DataGridCellsPresenter Grid.Column="1" Name="DGR_CellsPresenter" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Custom:DataGridDetailsPresenter Height="Auto" 
                                                          Grid.Column="1" 
                                                          Grid.Row="1" 
                                                          Visibility="{TemplateBinding DetailsVisibility}" Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type Custom:DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static Custom:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static Custom:SelectiveScrollingOrientation.Vertical}}" />
                    <!--<localprimitives:DataGridRowHeader localprimitives:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataGrid}}, Path=HeadersVisibility, Converter={x:Static local:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static local:DataGridHeadersVisibility.Row}}"/>-->
                    <Custom:DataGridRowHeader Grid.RowSpan="2" Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
                </Custom:SelectiveScrollingGrid>
            </Grid>
        </Grid>
    </ControlTemplate>
    <Style TargetType="Custom:DataGridRow">
        <Setter Property="Margin" Value="0, 0, 0, 0"/>
        <Setter Property="Template" Value="{StaticResource PlainDataGridRowControlTemplate}" />
    </Style>

有人可以提出建议吗?

1 个答案:

答案 0 :(得分:3)

我在DataGrid-Style中使用此setter解决了这个问题:

<Setter Property="ScrollViewer.CanContentScroll" Value="False" />

但是不要问我为什么这个适用于这个制定者。