我有一个具有TargetNullValue的数据绑定按钮:
<Button Content="{Binding Path=NextItem,Mode=OneWay,TargetNullValue='None'}" />
这工作正常,当NextItem为null时,按钮显示“无”.但我想将null值绑定到某个东西.
为简化起见,我们假设我的ViewModel上有一个名为NullValue的属性,它返回一个我希望在null时绑定的字符串:
<Button Content="{Binding Path=NextItem,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,TargetNullValue={Binding Path=NullValue,Source={StaticResource appResources}}}" />
这也引发了同样的异常.有什么建议?
解决方法
我在Connect上为此创建了一个问题.如果有人遇到这个问题,请将错误付诸表决.