我想要做的是基于复选框切换折叠WPF UI的底部部分。迄今为止,这大多按预期工作。在您逐步浏览图片时,您会看到一旦网格分离器被移动,折叠就停止工作。我不明白为什么或如何解决这个问题。wpf切换可见性崩溃只能工作一次
应用程序的开始显示正确。
切换复选框和botttom部分消失预期。
切换复选框一次,然后移动分割垂直,出现预期的一切。
现在切换复选框,最后一次,你会发现顶部部分不填写申请喜欢它曾经之前。这是它破裂的地方!我希望黄色部分可以像在初始切换中那样填充UI。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="525"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<CheckBox Name="ToggleVisibility" Margin="10" IsChecked="True" Content="Toggle Bottom Section"></CheckBox>
<StackPanel Background="#feca00" Grid.Row="1">
<TextBlock FontSize="20" Foreground="#58290A">Top Section</TextBlock>
</StackPanel>
<GridSplitter Grid.Row="2" Height="5" HorizontalAlignment="Stretch">
<GridSplitter.Style>
<Style TargetType="GridSplitter">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ToggleVisibility, Path=IsChecked}" Value="False">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</GridSplitter.Style>
</GridSplitter>
<StackPanel Grid.Row="3" Background="LightBlue">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ToggleVisibility, Path=IsChecked}" Value="False">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize="20" Foreground="black">Bottom Section</TextBlock>
</StackPanel>
</Grid>
</Window>
会有一种方法来重置Grid.Row高度是最大的可以而不是rowspan? – JokerMartini
您可以使用样式来设置'RowDefinition.Height'属性,并在复选框未选中时使用类似的触发器将其设置为'0',但它看起来像分隔符使用本地值设置高度,该值位于设置的值之前风格,所以它会“打破”反正。请参见[依赖属性值优先级](https://msdn.microsoft.com/en-us/library/vstudio/ms743230(v = vs.100).aspx)。 – Grx70
是的,我实际上只是试图,它不工作。似乎是一个合理的解决方案,但突破 – JokerMartini