我有一个数据绑定的按钮,它有一个TargetNullValue:
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue='None'}" />
这样做可以正常工作,当NextItem为null时,按钮会显示“None。”但是我想将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}}}" />
这也会抛出相同的异常。有什么建议吗?