6

一旦使用 gridsplitter 调整网格大小,当其他行折叠时,行 * 将不会回收空间。

我在具有三行的主详细信息视图中有以下网格。顶部的数据网格,中间的拆分器和最后一行的内容控件视图。拆分器上有一个关闭按钮,用于折叠细节。这一切都有效,但一旦用户使用 gridsplitter 调整大小。

    <Grid Margin="3,0">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Style="{StaticResource CollapsableRow}"/><!-- Splitter Here -->
        <RowDefinition Style="{StaticResource CollapsableRow}"/>
    </Grid.RowDefinitions>

GridSplitter 样式:

    <Style x:Key="gridSplitterStyle" TargetType="{x:Type GridSplitter}">
    <Setter Property="Visibility" Value="{Binding IsItemSelected, Converter={StaticResource BoolToShow},ConverterParameter='Visible|Collapsed'}" />
    <Setter Property="Width" Value="Auto"/>
    <Setter Property="Height" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Border.BorderBrush" Value="#FF6593CF" />
    <Setter Property="Border.BorderThickness" Value="0,1,0,0" />
    <Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
    <Setter Property="UIElement.Focusable" Value="False" />
    <Setter Property="Control.Padding" Value="7,7,7,7" />
    <Setter Property="Cursor" Value="SizeNS" /></Style>

就像我说的那样,除非使用 gridsplitter 调整大小,否则折叠工作正常。之后,空白保留。

编辑: HB 和 codenaked 有简单而一致的建议,所以我试图在数据触发器中实现它们而没有成功:

<Style x:Key="CollapsableRow" TargetType="{x:Type RowDefinition}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="True">
            <Setter Property="RowDefinition.Height" Value="0"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="False">
            <Setter Property="RowDefinition.Height" Value="Auto"/>
        </DataTrigger>            
    </Style.Triggers>
</Style>   
4

4 回答 4

7

由于网格拆分器和细节已经被隐藏,可见性是重置下一行定义高度的明显选择。

 /// <summary>
/// Grid splitter that show or hides the following row when the visibility of the splitter is changed. 
/// </summary>
public class HidableGridSplitter : GridSplitter { 

    GridLength height;

    public HidableGridSplitter()
    {
        this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged;
        this.Initialized += HideableGridSplitter_Initialized;
    }

    void HideableGridSplitter_Initialized(object sender, EventArgs e)
    {
        //Cache the initial RowDefinition height,
        //so it is not always assumed to be "Auto"
        Grid parent = base.Parent as Grid;
        if (parent == null) return;
        int rowIndex = Grid.GetRow(this);
        if (rowIndex + 1 >= parent.RowDefinitions.Count) return;
        var lastRow = parent.RowDefinitions[rowIndex + 1];
        height = lastRow.Height;
    }

    void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        Grid parent = base.Parent as Grid;
        if (parent == null) return;

        int rowIndex = Grid.GetRow(this);

        if (rowIndex + 1 >= parent.RowDefinitions.Count) return;

        var lastRow = parent.RowDefinitions[rowIndex + 1];

        if (this.Visibility == Visibility.Visible)
        {
            lastRow.Height = height;
        }
        else
        {
            height = lastRow.Height; 
            lastRow.Height = new GridLength(0);
        }

    }
于 2011-05-12T10:04:16.497 回答
3

您可以使用动画来解决gridsplitter对行/列定义的覆盖。在GridSplitter 覆盖 ColumnDefinition 的样式触发器中查看我对类似问题的回答?

于 2012-11-16T09:22:58.287 回答
1

如果您使用 GridSplitter,高度不再是Auto具体值。您需要使用样式或事件和后面的代码手动更改值,例如,这会在双击时重置自动大小列:

private void ColumnSplitter_DoubleClick(object sender, MouseButtonEventArgs e)
{
    if (!ColumnTreeView.Width.IsAuto) ColumnTreeView.Width = new GridLength();
}
于 2011-05-10T19:22:10.777 回答
0

根据您提供的内容,GridSplitter 将调整前一行和下一行的大小。您可以使用以下代码看到这一点:

<Grid Margin="3,0">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="row0" Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition x:Name="row2" Height="Auto" />
    </Grid.RowDefinitions>
    <Border Background="Red" >
        <TextBlock Text="{Binding ElementName=row0, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Border>
    <GridSplitter Grid.Row="1" Style="{StaticResource gridSplitterStyle}" HorizontalAlignment="Stretch" />
    <Border Background="Blue" Grid.Row="2" MinHeight="50">
        <TextBlock Text="{Binding ElementName=row2, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Border>
</Grid>

最后一行的大小实际上将从 Auto 更改为固定高度。因此即使折叠该行中的内容,它仍然会占用指定的空间。您需要将该行重置Height="Auto"为真正折叠它的内容。

于 2011-05-10T19:24:37.387 回答