我有以下wpf代码。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=toggleButtonShowGeneratedCode, Path=IsChecked}" Value="false">
<Setter Property="Width" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Button Content="Button 1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" Width="5" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"/>
<Grid Grid.Column="2">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=toggleButtonShowGeneratedCode, Path=IsChecked}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Button Content="Button 2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
</Grid>
<ToggleButton Grid.Row="1" Name="toggleButtonShowGeneratedCode" Width="50" Content="Toggle"/>
</Grid>
切换按钮应该折叠并展开按钮2.当按钮2折叠时,按钮1应占用其空间。
这在启动时有效,但是在拆分器移动后停止工作。
答案 0 :(得分:0)
移动GridSplitter时,它会强制更改上一列和下一列的宽度。它们不再是声明的*
和Auto
,而是*
和N
,其中N是某个数字。因此,当内容崩溃时,#2列不会崩溃。
建议的解决方案:使用一些代码隐藏功能将列#2宽度恢复为Auto
(适用于视图交互)
<Grid Grid.Row="0" Name="ResizableGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!--...-->
<ToggleButton Grid.Row="1" Name="toggleButtonShowGeneratedCode"
Width="50" Content="Toggle"
Unchecked="toggleButtonShowGeneratedCode_Unchecked"/>
private void toggleButtonShowGeneratedCode_Unchecked(object sender, RoutedEventArgs e)
{
ResizableGrid.ColumnDefinitions[2].Width = new GridLength(1, GridUnitType.Auto);
}
请注意,我添加了Grid for Grid以从代码
访问它