你能否将GroupBox的可见性绑定到它的孩子的可见性?

我有一个问题试图让GroupBox崩溃。 我想要一个GroupBox,如果它的所有子节点都被折叠,它将崩溃。

我已经设法通过对属性进行多重绑定来实现这一点,如下所示。

          

这个问题是我们希望能够多次执行此操作而不必担心不使用绑定。 所以我的问题是有什么方法可以做到这一点,最好是一种风格。 另一个要求是它必须是xaml而不是代码。

所以我的理想答案是风格,所以我可以在我的xaml中使用以下内容。

     

我看过这些问题,他们让我认为这是不可能的; controltemplate中的 绑定 , stackpanel可见性 , 边界可见性 。

对不起,如果之前已经回答过。 提前感谢您的任何答案/评论。

折叠子项时,可以使用MultiDataTrigger折叠GroupBox

这是一个工作示例:

                  

有两种方法,都有附加行为:第一种是在父GroupBox和OnPropertyChanged回调循环上设置所有子项的附加属性,并将绑定添加到多绑定,然后挂钩到GroupBox Visibility属性。 这种方法的问题在于您必须指定要包含在多重绑定中的子(ren)的类型(因为您需要找到它们以将它们添加到指示父级状态的组中) – FindVisualChildren需要使用多种generics类型进行调用,如果你想捕获你想要的所有东西……可以轻松完成:

 public sealed class GroupBoxCloseBehavior : DependencyObject { public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GroupBoxCloseBehavior), new PropertyMetadata(false, OnIsEnabledChanged)); public static bool GetIsEnabled(DependencyObject obj) { return (bool)obj.GetValue(IsEnabledProperty); } public static void SetIsEnabled(DependencyObject obj, bool value) { obj.SetValue(IsEnabledProperty, value); } private static void OnIsEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { GroupBox parent = obj as GroupBox; if (parent == null) { return;//Do nothing, or throw an exception depending on your preference } if (parent.IsLoaded) { MultiBinding mb = new MultiBinding(); mb.Converter = new MultiVisibilityToVisibilityConverter(); if ((bool)e.NewValue) { foreach (CheckBox child in FindVisualChildren(parent)) { mb.Bindings.Add(new Binding("Visibility") { Mode = BindingMode.OneWay, Source = child }); } BindingOperations.SetBinding(parent, UIElement.VisibilityProperty, mb); } else { BindingOperations.ClearBinding(parent, UIElement.VisibilityProperty); } } else { parent.Loaded += (sender, eventArgs) => { MultiBinding mb = new MultiBinding(); mb.Converter = new MultiVisibilityToVisibilityConverter(); if ((bool)e.NewValue) { foreach (CheckBox child in FindVisualChildren(parent)) { mb.Bindings.Add(new Binding("Visibility") { Mode = BindingMode.OneWay, Source = child }); } BindingOperations.SetBinding(parent, UIElement.VisibilityProperty, mb); } else { BindingOperations.ClearBinding(parent, UIElement.VisibilityProperty); } }; } } private sealed class MultiVisibilityToVisibilityConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return values.OfType().Any(vis => vis != Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren(child)) { yield return childOfChild; } } } } }             

换句话说,在子元素上放置附加属性,在树上寻找父GroupBox的OnPropertyChanged可能会更好,但是您不必知道有多少元素。 这只是绑定的限制。 至少使用GroupBox附加属性,您可以构建所需的绑定。