0

我试图在单击 aGrid时隐藏 a 的前两列Button。我的Grid布局有 3 列,一列带有网格,第二列带有网格拆分器,第三列Grid带有Button.

当我使用下面的代码运行我的程序时,它会按预期折叠前两列Button并调整第三个网格的大小,但是当我使用拆分器调整网格大小时,这不再起作用。它隐藏了列,但是第三列未调整大小以填充Window. 我希望前 2 列折叠,第三列填充窗口的整个区域(如果我不使用拆分器调整大小,则会发生这种情况)。

xml如下:

<Grid>
    <ColumnDefinition Width="Auto" x:Name="column1"/>
    <ColumnDefinition Width="Auto" x:Name="column2"/>
     <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Grid Grid.Column="0" x:Name="left" MinWidth="100">
    <Border Background="Red" Margin="5"/>
    <TextBlock Text="A Brown fox jumped oversomething" Width="{Binding ActualWidth, ElementName=TreeView}" Margin="5"></TextBlock>
</Grid>

<GridSplitter x:Name="splitter"
            Width="5"
            Grid.Column="1"
            HorizontalAlignment="Left"
            Margin="0,5,0,5"
            Panel.ZIndex="1"
            VerticalAlignment="Stretch"                
            ResizeDirection="Columns"/>

<Grid Grid.Column="2">        
    <Grid Grid.Column="0" Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>
        <Border Grid.Row="0" Background="Green" Margin="5"/>
        <Button Grid.Row="1" Click="OnClick">HideAndResize</Button>
    </Grid>
</Grid>
</Grid>

事件Button.click处理如下:

private bool clicked;

private void OnClick(object sender, RoutedEventArgs e)
{
    clicked = !clicked;

    left.Visibility = clicked ? Visibility.Collapsed : Visibility.Visible;
    splitter.Visibility = clicked ? Visibility.Collapsed : Visibility.Visible;

}
4

1 回答 1

2

似乎 Column 没有正确自动调整大小,所以它仍然不是 0,即使它的子 Visibility 设置为 Collapsed。

一个快速而肮脏的解决方案是:

    private bool clicked;
    private double oldLenght;
    private void OnClick(object sender, RoutedEventArgs e)
    {
        clicked = !clicked;

        splitter.Visibility = clicked ? Visibility.Collapsed : Visibility.Visible;
        left.Visibility = clicked ? Visibility.Collapsed : Visibility.Visible;
        oldLenght = clicked ? column1.ActualWidth : oldLenght;
        column1.Width = clicked ? new GridLength(0.0) : new GridLength(oldLenght);
    }
于 2013-01-08T13:09:21.967 回答