This project has moved. For the latest updates, please go here.
1
Vote

Issues with converters when value is DependencyObject.UnsetValue

description

Basically the converters fail with a cast exception when the value is DependencyObject.UnsetValue.

https://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.unsetvalue%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Basically this value could be passed as value when you are binding for example to a binding that cannot be properly resolved at a certain point in time but that does get resolved in the future.
I had this problem with the VisibilityToBooleanConverter for example.

It can be easily solved by adding this as first line for all converters (or at least where it makes sense, which is usually most of them):

if (value == DependencyObject.UnsetValue)
return DependencyObject.UnsetValue;

Also while at it, maybe add to VisibilityToBooleanConverter (and also to BooleanToVisibilityConverter) a property that lets you pick if a bool == false is Collapsed (default) or Hidden

comments

xaviergonz wrote Jan 31, 2016 at 2:50 AM

Sorry where it says:
DependencyObject.UnsetValue
should be
DependencyProperty.UnsetValue

xaviergonz wrote Jan 31, 2016 at 3:01 AM

And maybe for the sake of completeness add to each converter an attribute such as

[ValueConversion(typeof(Visibility), typeof(bool))]

I think the designer uses this info to know if the converter is proper for the type. but I'm not sure.