“嵌套”属性绑定为null会发生什么?

时间:2011-05-24 00:14:34

标签: silverlight

示例:

Text =“{Binding Path = CurrentPerson.FirstName}”

当CurrentPerson为空时会发生什么?这是一个执行吗?

1 个答案:

答案 0 :(得分:2)

它不会抛出异常,它会在运行时计算表达式时静默失败。

当source为null时,您可以使用TargetNullValue属性提供值:

<TextBox Width="150"
     Text="{Binding Source={StaticResource object}, 
     Path=PropertyB, BindingGroupName=bindingGroup, 
     TargetNullValue=please enter a string}" />

http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue.aspx

此外,当属性路径无效时,您可以使用FallbackValue:

<TextBlock Text="{Binding Path=BadPath, FallbackValue='Invalid Path'}"/>
相关问题