你能把GroupBox的可见性绑定到它的孩子的可见性上吗?我在尝试使GroupBox崩溃时遇到问题。我想要一个GroupBox,如果它的所有子项都折叠,它就会折叠。我已经设法通过对属性进行多重绑定来实现这一点,如下所示。这样做的问题是我们希望能够多次执行此操作而不必担心不使用绑定。所以我的问题是有没有办法做到这一点,最好是有一种风格。另一个要求是它必须是xaml而不是代码。所以我理想的答案是样式,这样我就可以在我的xaml中使用以下内容。我看过这些问题,它们让我觉得这是不可能的;在controltemplate中绑定、堆栈面板可见性、边框可见性。抱歉,如果之前已经回答过这个问题。在此先感谢您的任何回答/评论。折叠子项时,您可以使用MultiDataTrigger折叠GroupBox这是一个工作示例:有两种方法,都具有额外的行为:第一种是在父GroupBox和OnPropertyChanged回调循环上设置所有子项的附加属性,以及将Add绑定到多重绑定,然后挂接到GroupBoxVisibility属性。这种方法的问题是您必须指定要包含在多重绑定中的子项(ren)的类型(因为您需要找到它们以将它们添加到指示父级状态的组中)-FindVisualChildren需要多个泛型类型拨打电话,如果你想捕获你想要的一切......它可以很容易地完成:publicsealedclassGroupBoxCloseBehavior:DependencyObject{publicstaticreadonlyDependencyPropertyIsEnabledProperty=DependencyProperty.RegisterAttached("IsEnabled",typeof(bool),typeof(GroupBoxCloseBehavior),newPropertyMetadata(false,OnIsEnabledChanged));publicstaticboolGetIsEnabled(DependencyObjectobj){return(bool)obj.GetValue(IsEnabledProperty);}publicstaticvoidSetIsEnabled(DependencyObjectobj,boolvalue){obj.SetValuedperty(Is,EnabledProperty);}privatestaticvoidOnIsEnabledChanged(DependencyObjectobj,DependencyPropertyChangedEventArgse){GroupBoxparent=objasGroupBox;if(parent==null){return;//什么也不做,或者根据你的偏好抛出异常}s(parent.I){MultiBindingmb=newMultiBinding();mb.Converter=新的MultiVisibilityToVisibilityConverter();if((bool)e.NewValue){foreach(CheckBoxchildinFindVisualChildren(parent)){mb.Bindings.Add(newBinding("Visibility"){Mode=BindingMode.OneWay,Source=child});}BindingOperations.SetBinding(parent,UIElement.VisibilityProperty,mb);}else{BindingOperations.ClearBinding(parent,UIElement.VisibilityProperty);}}else{parent.Loaded+=(sender,eventArgs)=>{MultiBindingmb=newMultiBinding();mb.Converter=newMultiVisibilityToVisibilityConverter();if((bool)e.NewValue){foreach(CheckBoxchildinFindVisualChildren(parent)){mb.Bindings.Add(newBinding("Visibility"){Mode=BindingMode.OneWay,Source=child});}BindingOperations.SetBinding(parent,UIElement.VisibilityProperty,mb);}else{BindingOperations.ClearBinding(parent,UIElement.VisibilityProperty);}};}}私有密封类MultiVisibilityToVisibilityConverter:IMultiValueConverter{publicobjectConvert(object[]values,TypetargetType,对象参数,System.Globalization.CultureInfoculture){returnvalues.OfType().Any(vis=>vis!=Visibility.Collapsed)?Visibility.Visible:Visibility.Collapsed;}publicobject[]ConvertBack(objectvalue,Type[]targetTypes,objectparameter,System.Globalization.CultureInfoculture){thrownewNotSupportedException();}}publicstaticIEnumerableFindVisualChildren(DependencyObjectdepObj)whereT:DependencyObject{if(depObj!=null){for(inti=0;i(child)){yieldreturnchildOfChild;}}}}}换句话说,在子元素上放置一个附加属性可能会更好,在树中查找父GroupBox的OnPropertyChanged,但您不必知道有多少个元素这只是一个绑定的限制。至少使用GroupBox附加属性,您可以构建所需的绑定。C#教程就是这些:能否将GroupBox的可见性绑定到其子项的可见性?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
