8

I have a Grid whose Visibility property is bound to a boolean property of a certain model using a Converter:

<Grid Visibility="{Binding ElementName=MyTreeView, Path=SelectedItem.MyBoolProperty, Converter={StaticResource boolToVisConverter}}">
    <!-- child elements -->
</Grid>

It works great when an element in my TreeView is selected, but if nothing is selected or the TreeView is empty it defaults to being visible. I need it to be hidden by default. I've tried using TargetNullValue=Hidden but it isn't working. I guess I just don't understand how the TargetNullValue property is supposed to work in this situation.

Does anybody have any ideas how to get the functionality I am looking for?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
jebar8
  • 2,113
  • 3
  • 21
  • 31

1 Answers1

18

I spent hours trying to figure this out. Then, of course, right after I post the question I get it working using FallbackValue=Hidden on the Binding property of the Grid.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
jebar8
  • 2,113
  • 3
  • 21
  • 31
  • Thanks! This was a life saver. I tried TargetNullValue and Triggers as suggested by http://stackoverflow.com/questions/5474586/wpf-datatrigger-visibility-null-value and they did not work. Any ideas why this works and not other solutions? – Dave Apr 15 '13 at 22:30
  • @Dave Thats because TargetNullValue applies only for the binding target property (the last element in binding path). So the TargetNullValue on target property of type bool has absolutely no sense since the bool cannot be null anyway. – Lukáš Koten Nov 27 '20 at 16:43