2011-02-10 66 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