【发布时间】:2010-11-14 22:04:39
【问题描述】:
我有一个具有 TargetNullValue 的数据绑定按钮:
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue='None'}" />
效果很好,当 NextItem 为空时,按钮显示“无”。但我想将 null 值绑定到某个东西。
为了简单起见,假设我的 ViewModel 上有一个名为 NullValue 的属性,它返回一个字符串,当它为 null 时,我希望绑定到该字符串:
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={Binding Path=NullValue}}" />
但这会引发:
System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception. [Line: 129 Position: 92]
---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at MS.Internal.XamlManagedRuntimeRPInvokes.TryApplyMarkupExtensionValue(Object target, XamlPropertyToken propertyToken, Object value)
at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)
我真正想做的是将 null 值绑定到 resx,所以我在控件的资源字典中有它,并像这样更改绑定:
<res:AppResources x:Key="appResources" />
...
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={Binding Path=NullValue,Source={StaticResource appResources}}}" />
这也会引发相同的异常。有什么建议吗?
【问题讨论】:
标签: data-binding silverlight-4.0 resources