Search code examples
c#wpfdata-bindingdatatrigger

In WPF, is the FallbackValue used when the binding fails due to null references?


My view-model exposes a list called MyList that may be empty or null. I have an element that I would like hide based on this state. If MyList is empty or null, then the element should be collapsed. If it has elements then it 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 to this DataTrigger when MyList is null?
  • Will it use the FallbackValue or will it fail?
  • Is this documented somewhere?

Solution

  • The FallbackValue is used if the binding source path does not resolve, if the converter fails, or if the value is not valid for the property's type.

    It will not be used if null is returned, unless null is not valid for the property type. In this case the DataTrigger will not be triggered. 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>