views:

29

answers:

2

How do I turn off a custom IValueConverter at design time? Basically I want to write this:

Public Class MethodBinder
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        If [DESIGN_TIME] Then Return Nothing
        If value IsNot Nothing Then Return CallByName(value, CStr(parameter), CallType.Method)
        Return Nothing
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotSupportedException
    End Function
End Class
A: 

The only one I know needs an UIElement:

DesignerProperties.GetIsInDesignMode(UIElement element);
Goblin
A: 

I worked around this to react specifically to the incoming (default) value.

In my example I was using a library loaded at runtime to calculate the number of business days based on the incoming offset (using an extension method), this would fail in design-time because the library wasn't loaded.

But the default value for the control was 0, so

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    int offset = 0;
    if(value is double)
        offset = (int)(double)value;

    DateTime dt = offset == 0
        ? DateTime.Today.Date.AddDays(-offset) 
        : DateTime.Today.Date.AddBusinessDays(-offset) ;

    return string.Format("{0} ({1})", (offset), dt.ToString("ddd dd-MMM"));
}

It's not pretty, but it was effective.

Unsliced