调整视图的可见性属性不起作用 [英] Adjust the Visibility property of a View not working

查看:101
本文介绍了调整视图的可见性属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将布尔属性绑定到视图的Visibility属性时遇到了一个奇怪的问题。

I've a strange problem with binding a boolean property to a View's Visibility property.

我有一个主视图,其中包含许多其他视图,例如以及其他各种UIElement,包括按钮,文本框,网格,StackPanels和一些telerik控件。某些控件的可见性与我的ViewModel上的boolean属性有关,因此当该属性为正时,它们将被显示,而当其为负时,它们将被折叠。

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}}" >

这对我来说非常有效。显然,我已经在 IsSectionShown 设置器中触发了通知事件,并且控件的可见性也进行了相应的调整。

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}}" />

不起作用!我的解决方法是将视图包装在StackPanel中并调整StackPanel的可见性-可以正常工作:

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.

推荐答案

要使这两个方法均有效,请使用 IsPositive 属性必须同时存在于 Activity 内部和在 Activity 来自。但这可能不是您想要的。相反,您可以使用类似这样的方法,以便可见性来自与应用于视图本身不同的数据上下文:

For both of these to work, the IsPositive property would have to exist both inside Activity and one level up in the data context that Activity 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:

<vw:ActivityView DataContext="{Binding Activity}" 
                 Visibility="{Binding IsPositive, ElementName=ParentElement,
                      Converter={StaticResource BoolToVisibilityConverter}}" />

其中 ParentElement 是包含以下内容的父元素 vw:ActivityView

where ParentElement is the parent element that contains vw:ActivityView.

这篇关于调整视图的可见性属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆