Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? Workaround is fine but I would like to identity the gap in my understanding.

like image 617
Kirk Broadhurst Avatar asked Nov 06 '22 03:11

Kirk Broadhurst


1 Answers

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.

like image 155
Rick Sladkey Avatar answered Nov 11 '22 09:11

Rick Sladkey