2011-02-10 61 views
2

讓我們說我有以下代碼:價值轉換器。力WPF調用它只是一個時間

<ContextMenu IsEnabled="{Binding Converter={StaticResource SomeConverterWithSmartLogic}}"> 

所以,我並沒有指定的任何綁定信息,除了轉換器...是否有可能迫使WPF調用它只是一個時間?

UPD:這一刻我存儲在靜態字段中

+0

是否有理由選擇解決這個問題而不是解決全局變量的問題?H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hstatic字段用法? – 2011-02-10 12:06:38

+0

恕我直言,這應該只是在ViewModel完成,並一起刪除轉換器;在這種情況下轉換器不應該是智能 – 2011-02-10 15:09:02

回答

1

如果你的轉換器轉換器應該一次只你可以寫你的轉換器是這樣,如果不引起其他障礙,至少不需要靜態字段等的例如

[ValueConversion(typeof(double), typeof(double))] 
public class DivisionConverter : IValueConverter 
{ 
    double? output; // Where the converted output will be stored if the converter is run. 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (output.HasValue) return output.Value; // If the converter has been called 
                // 'output' will have a value which 
                // then will be returned. 
     else 
     { 
      double input = (double)value; 
      double divisor = (double)parameter; 
      if (divisor > 0) 
      { 
       output = input/divisor; // Here the output field is set for the first 
              // and last time 
       return output.Value; 
      } 
      else return DependencyProperty.UnsetValue; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 
1

值轉換器的狀態,你試過設置結合一次性?

+0

是的,但它並沒有幫助 – 2011-02-10 11:53:34

相關問題