绑定StackPanel.Visibility其子项的可见性属性 [英] Bind StackPanel.Visibility to the Visibility property of its children
问题描述
我是比较新的数据绑定和只是阅读到它。
我想要做的是以下内容:
I'm relatively new to DataBinding and just reading into it. What I want to do is the following:
我有一个StackPanel中与多家子控件的:
I have a StackPanel with a number of child controls:
<StackPanel Orientation="Horizontal">
<TextBox x:Name="textbox1" Width="100">1</TextBox>
<TextBox x:Name="textbox2" Width="100">2</TextBox>
<TextBox x:Name="textbox3" Width="100">3</TextBox>
</StackPanel>
文本框的可见性属性可以通过代码进行更改。
现在,如果所有的文本框都设置为可见=瓦解了,我也想StackPanel.Visibility设置为折叠,但如果显示一个或多个文本框(能见度=可见),StackPanel.Visibility也应设置为可见。
The visibility property of the textboxes can be changed by code. Now, if all TextBoxes are set to Visibility=Collapsed, i also want StackPanel.Visibility set to Collapsed, but if one or more TextBoxes are shown (Visibility=Visible), StackPanel.Visibility should also be set to Visible.
可以这样用一个简单的数据绑定实现了或者我必须实现在C#代码此功能?
Can this be achieved with a simple DataBinding or do I have to implement this functionality in C# code?
推荐答案
我不能想办法直接通过数据绑定做到这一点。
I can not think of a way to do this directly through databinding.
我个人有看法的背后视图模型,并设置视图的DataContext到视图模型。
Personally I would have a view model behind the view, and set the views DataContext to the view model.
在视图模型我会再有一个属性告诉,如果所有的文本框被折叠的看法。该属性将被代码进行设置。然后绑定StackPanel的知名度,该属性。
In the view model I would then have a property telling the view if all the textboxes are collapsed. That property would be set by code. Then bind the stackpanel visibility to that property.
(该属性必须是一个依赖关系属性,或视图模型必须实现INotifyPropertyChanged为视图自动更新)
(The property must either be a dependancy property, or the view model must implement INotifyPropertyChanged for the view to automatically update)
这篇关于绑定StackPanel.Visibility其子项的可见性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!