问题描述
如果每个子项都折叠,我想显示/折叠面板(Stakpanel、Grid 等),如果其子项再次可见,则至少显示一个面板.
I would like to show/collapse a panel (Stakpanel, Grid etc) if every children is collapsed and show it back if at least one if its children is visible again.
实现这一目标的最佳方法是什么?(转换器,触发器,其他东西?)谢谢!!
Which would be the best way to achive this? (converter, triggers, other thing?)Thanks!!
我做了一个转换器,但当我改变孩子的可见性时它不会触发
I did a converter but it doesn´t fire when I change the visibility of children
public class HasChildrenVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Panel parent = value as Panel;
foreach (var child in parent.Children.OfType<UIElement>())
{
if (child.IsVisible)
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;//no implementado
}
}
这是xaml示例
<Window.Resources>
<local:HasChildrenVisibilityConverter x:Key="converter" />
</Window.Resources>
<StackPanel Visibility="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource converter}}">
<TextBlock x:Name="text1"> Text 1</TextBlock>
<TextBlock x:Name="text2"> Text 2</TextBlock>
</StackPanel>
推荐答案
MultiDataTrigger
在这里工作得很好.这是一个带有 StackPanel
和几个 TextBlock
的简单示例我在样式中声明触发器并将该样式应用于相关的 StackPanel
A MultiDataTrigger
would work quite well here. Here's a simple example with a StackPanel
and a couple of TextBlock
I declare the Triggers in the Style and apply that Style to the relevant StackPanel
主窗口.xaml
<Window.Resources>
<Style x:Key="ShowHideStyle" TargetType="StackPanel">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=txtName, Path=Visibility}" Value="Collapsed" />
<Condition Binding="{Binding ElementName=txtDescription, Path=Visibility}" Value="Collapsed" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Collapsed"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=txtName, Path=Visibility}" Value="Visible" />
<Condition Binding="{Binding ElementName=txtDescription, Path=Visibility}" Value="Visible" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Visible"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel Style="{StaticResource ShowHideStyle}" Height="300" Width="300" Background="Red">
<TextBlock x:Name="txtName" Text="name" />
<TextBlock x:Name="txtDescription" Text="description" />
</StackPanel>
<Button x:Name="btnHide" Width="100" Height="30" Content="hide" Click="btnHide_Click"/>
<Button x:Name="btnShow" Width="100" Height="30" Content="show" Click="btnShow_Click"/>
</StackPanel>
主窗口.xaml.cs我只是添加按钮点击事件来隐藏/显示子元素
Mainwindow.xaml.cs I just add the button click events to hide/show child elements
private void btnHide_Click(object sender, RoutedEventArgs e)
{
txtDescription.Visibility = System.Windows.Visibility.Collapsed;
txtName.Visibility = System.Windows.Visibility.Collapsed;
}
private void btnShow_Click(object sender, RoutedEventArgs e)
{
txtDescription.Visibility = System.Windows.Visibility.Visible;
txtName.Visibility = System.Windows.Visibility.Visible;
}
这篇关于如果所有子项都折叠,我如何折叠父内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!