开发者

Adjust the Visibility property of a View not working

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? Work开发者_C百科around is fine but I would like to identity the gap in my understanding.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜