Adsense

Saturday, October 15, 2011

WPF Visibility Binding Not Working

I was playing around with WPF and binding to the Visibility property of a Grid. The examples seemed pretty straight forward, but for the life of me, I couldn’t get it to work! My test was setup like the following.
I was correctly referencing the BooleanToVisibilityConverter as a static resource.

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisiblity" />
</Window.Resources>
 
My binding was correctly set to the IsControlVisible property of my ViewModel.
 
<Grid Margin="0,30,0,0" Height="63" VerticalAlignment="Top" 
 Visibility="{Binding IsControlVisible, 
 Converter={StaticResource BoolToVisiblity}}">
    <!--Content-->
</Grid>
 
The IsControlVisible property of my ViewModel seemed to be correct.
 
bool isControlVisible = false;
bool IsControlVisible {
 get { return isControlVisible; }
 set {
  isControlVisible = value;
  NotifyPropertyChanged("IsControlVisible");
 }
}
 
20 frustrating minutes later, I find the problem. 10 bonus points if you can spot it right away…

That’s right, I forgot to make the IsControlVisible property in my ViewModel public! *Face Palm*
 
bool isControlVisible = false;
public bool IsControlVisible {
 get { return isControlVisible; }
 set {
  isControlVisible = value;
  NotifyPropertyChanged("IsControlVisible");
 }
}

Perhaps this can save someone else some time!

2 comments:

Anonymous said...

try Converter={StaticResource BooleanToVisiblity}}">

Anonymous said...

Thk U, that did me check and I just had that property as private.