在WPF中,由于空引用导致绑定失败时使用的FallbackValue是什么?

时间:2015-10-29 14:10:35

标签: c# wpf data-binding datatrigger

我的视图模型公开了一个名为MyList的列表,该列表可能为空或null。我有一个基于这个状态隐藏的元素。如果MyList为空或null,则该元素应折叠。如果它有元素则应显示。

这是我的DataTrigger

<DataTrigger Binding="{Binding MyList.Count, FallbackValue=0}" Value="0">
    <Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
  • DataTriggerMyList时,此null会发生什么?
  • 它会使用FallbackValue还是会失败?
  • 这是在某处记录的吗?

1 个答案:

答案 0 :(得分:15)

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>