Does WPF use FallbackValue when binding fails due to null references?

My view model provides a list called MyList , which can be empty or null . I have an element that I would like to hide based on this state. If MyList empty or null , then the item should be collapsed. If it has elements, then this should be shown.

Here is my DataTrigger :

 <DataTrigger Binding="{Binding MyList.Count, FallbackValue=0}" Value="0"> <Setter Property="Visibility" Value="Collapsed"></Setter> </DataTrigger> 
  • What happens with this DataTrigger when MyList is null ?
  • Will FallbackValue be used, or will it not work?
  • Is it somewhere documented?
+10
source share
1 answer

FallbackValue used if the binding source path is not resolved, if the converter fails, or if the value is not valid for the property type.

It will not be used if null returned, if null not valid for this type of property. In this case, the DataTrigger will not DataTrigger . You can use TargetNullValue for this case.

 <DataTrigger Binding="{Binding MyList.Count, FallbackValue=0, TargetNullValue=0}" Value="0"> <Setter Property="Visibility" Value="Collapsed"></Setter> </DataTrigger> 
+14
source

Source: https://habr.com/ru/post/1234794/


All Articles