我的视图模型公开了一个名为MyList
的列表,该列表可能为空或null
。我有一个基于这个状态隐藏的元素。如果MyList
为空或null
,则该元素应折叠。如果它有元素则应显示。
这是我的DataTrigger
:
<DataTrigger Binding="{Binding MyList.Count, FallbackValue=0}" Value="0">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
DataTrigger
为MyList
时,此null
会发生什么? FallbackValue
还是会失败? 答案 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>