I have a databound button that has a TargetNullValue:
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue='None'}" />
That works fine, and when NextItem is null, the button shows “None.” But I want to bind the null value to something.
To simplify things, let’s assume that I have a property on my ViewModel called NullValue that returns a string that I want to be bound to when null:
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={Binding Path=NullValue}}" />
But this throws:
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)
What I really want to do is bind the null value to a resx, so I have that in the control’s resource dictionary and change the binding like so:
<res:AppResources x:Key="appResources" />
...
<Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={Binding Path=NullValue,Source={StaticResource appResources}}}" />
This also throws the same exception. Any suggestions?
I created an issue for this at Connect. If anyone out there comes across this, please vote up the bug.
https://connect.microsoft.com/VisualStudio/feedback/details/632602/silverlight-4-targetnullvalue-binding-to-resource