调整视图的可见性属性不起作用
我在将布尔属性绑定到视图的可见性属性时遇到了一个奇怪的问题。
我有一个“主”视图,其中包含许多其他视图以及各种其他 UIElements,包括按钮、文本框、网格、StackPanels 和一些 telerik 控件。某些控件的可见性绑定到我的 ViewModel 上的布尔属性,这样当属性为正时,它们就会显示,当属性为负时,它们就会折叠。
<Border Visibility="{Binding IsSectionShown,
Converter={StaticResource BoolToVisibilityConverter}}" >
这对我来说非常有效。显然我已经在 IsSectionShown
设置器中触发了通知事件,并且控件的可见性也相应地进行了调整。
现在我有一个需要调整其可见性的视图。明显的实现是
<vw:ActivityView DataContext="{Binding Activity}"
Visibility="{Binding IsPositive,
Converter={StaticResource BoolToVisibilityConverter}}" />
行不通的!我的解决方法是将我的视图包装在 StackPanel 内并调整 StackPanel 的可见性 - 这工作得很好:
<StackPanel Visibility="{Binding IsPositive,
Converter={StaticResource BoolToVisibilityConverter}}">
<vw:ActivityView DataContext="{Binding Activity}" />
</StackPanel>
关于为什么会发生这种情况有什么想法吗?解决方法很好,但我想找出我理解中的差距。
I've a strange problem with binding a boolean property to a View's Visibility property.
I have a 'main' View that contains a bunch of other Views as well as various other UIElements including Buttons, TextBoxes, Grids, StackPanels and some telerik controls. Some of the controls have their visibility bound to boolean properties on my ViewModel, such that when the property is positive they are shown, and when negative they are collapsed.
<Border Visibility="{Binding IsSectionShown,
Converter={StaticResource BoolToVisibilityConverter}}" >
This is working perfectly for me. Obvious I have trigger the notification event in the IsSectionShown
setter, and the control's Visibility is adjusted accordingly.
Now I have a View which needs to have its visibility adjusted. The obvious implementation is
<vw:ActivityView DataContext="{Binding Activity}"
Visibility="{Binding IsPositive,
Converter={StaticResource BoolToVisibilityConverter}}" />
Does not work! My work around is to wrap my view inside a StackPanel and adjust the visibility of the StackPanel - and this works fine:
<StackPanel Visibility="{Binding IsPositive,
Converter={StaticResource BoolToVisibilityConverter}}">
<vw:ActivityView DataContext="{Binding Activity}" />
</StackPanel>
Any ideas as to why this is happening? Workaround is fine but I would like to identity the gap in my understanding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使这两个功能都起作用,
IsPositive
属性必须同时存在于Activity
内部以及Activity
来源的数据上下文中的上一层。但这可能不是您想要的。相反,您可以使用类似的内容,以便可见性来自与应用于视图本身的数据上下文不同的数据上下文:其中
ParentElement
是包含vw:ActivityView< 的父元素/代码>。
For both of these to work, the
IsPositive
property would have to exist both insideActivity
and one level up in the data context thatActivity
comes from. But that's probably not what you intended. Instead, you can use something like this so that the visibility comes from a different data context than the one that applies to the view itself:where
ParentElement
is the parent element that containsvw:ActivityView
.