2010-06-15 19 views
0

如何在設計時關閉自定義IValueConverter?基本上我想這樣寫:如何在設計時關閉自定義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 

回答

0

我知道的唯一一個需要一個UIElement

DesignerProperties.GetIsInDesignMode(UIElement element); 
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")); 
} 

這不是很漂亮,但它是有效的。