点击Grid
时,我试图隐藏Button
的前两列。我的Grid
布局有3列,第二列有网格,第二列有网格分割符,第三列有另一个Grid
,其中Button
。
当我使用下面的代码运行我的程序时,它会按预期正确地点击Button
折叠前两列并调整第三个网格的大小,但是当我使用拆分器调整网格大小时,这样做不再工作了。它会隐藏列,但第三列不会调整大小以填充Window
。我希望折叠前2列,第三列填充窗口的整个区域(如果我不使用拆分器调整大小,则会发生这种情况)。
xaml如下:
<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;
}
答案 0 :(得分:2)
似乎列没有正确自动调整,所以它仍然不是0,即使它的子项可见性设置为折叠。
快速而肮脏的解决方案是:
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);
}