如何在设计时关闭自定义IValueConverter?

时间:2010-06-15 04:17:28

标签: xaml binding

如何在设计时关闭自定义IValueConverter?基本上我想写这个:

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

2 个答案:

答案 0 :(得分:1)

我解决了这个问题,专门对传入(默认)值做出反应。

在我的示例中,我使用在运行时加载的库来计算基于传入偏移量的工作日数(使用扩展方法),这将在设计时失败,因为未加载库。

但是控件的默认值是0,所以

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"));
}

它不漂亮,但它很有效。

答案 1 :(得分:0)

我认识的唯一一个需要UIElement

DesignerProperties.GetIsInDesignMode(UIElement element);