user164771
user164771

Reputation:

Default IValueConverter

I have a simple IValueConverter which just uses a TypeConverter to do the conversions. However there are some cases where the TypeConverter provided will fail.

I would like to fall back to whatever WPF would have used if the the Binding was not provided with a converter. Using reflector I have determined that there is a DefaultValueConverter however it is internal to MS.Internal.Data.

Is there a simple way I can get my hands on a DefaultValueConverter or something that does the same thing?

Upvotes: 6

Views: 1350

Answers (2)

Salvatore Previti
Salvatore Previti

Reputation: 9050

I looked a bit around with .NET reflector and it seems there is not an official way to get the default value converter.

What you need to be precise is to call the method

internal IValueConverter GetDefaultValueConverter(Type sourceType, Type targetType, bool targetToSource);

in class MS.Internal.Data.DataBindEngine.

You would get the current DataBindEngine for running thread with the static internal property

 internal static DataBindEngine CurrentDataBindEngine

Always in DataBindEngine class.

The problem is that the class is internal!

I looked also around to see if there is something public that uses that method but in reality that method and the default value converter classes (there is more than one!) are used only inside the internal databinding classes, so, no way out, sorry.

You could use reflection to access the internal members, but it would be a very bad dirty hack! To do that, access to the static property DataBindEngine.CurrentDataBindEngine, then, access to the method GetDefaultValueConverter, always with reflection. Wrap in a static function with the attribute [Obsolete] :)

Probably a cleaner way is to just use a default hand-written conversion.

You can just try understand why it gives you error or write a code that can handle the special cases. Their code is quite complicated and interlinked, I would not suggest to copy their code.

Sometime WPF makes me angry, a lot of things related to databinding are hidden, internal or not enough generic, and this is a really bad design choice that I don't understand.

The old TypeConverter sometime is enough. It depends on what you have to do.

public static object MyConvert(object value, Type t)
{
    TypeConverter tc = TypeDescriptor.GetConverter(t);
    return tc.ConvertFrom(value);
}

You can also try the static method Convert.ChangeType that works with IConvertible.

I guess the problem is not in converting your object but converting it into a value used by a dependency property (or viceversa). For this purpose there is the public class XamlValueConverter, used also internally by default value converters. This is going to be complicated however, so ... not a real simple solution to your problem.

As Merlyn keep saying, I'm starting to believe that reflection may be a more suitable way.

Upvotes: 2

Maheep
Maheep

Reputation: 5605

As DefaultValueConverter is internal to MS.Internal.Data the best is to create your own version of default value converter

Upvotes: 0

Related Questions