代码之家  ›  专栏  ›  技术社区  ›  Jonathan Allen

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

  •  0
  • Jonathan Allen  · 技术社区  · 14 年前

    如何关闭自定义 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 回复  |  直到 11 年前
        1
  •  1
  •   Unsliced    14 年前

    我在这方面做了一些工作,专门对传入(默认)值做出反应。

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

    但是控件的默认值是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"));
    }
    

    它不漂亮,但很有效。

        2
  •  0
  •   Dave Clemmer manu    11 年前

    我知道的唯一一个需要 UIElement :

    DesignerProperties.GetIsInDesignMode(UIElement element);