2

次のコードがあるとしましょう:

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

したがって、Converter以外のバインディング情報を指定しませんでした... WPFに1回だけ呼び出すように強制することは可能ですか?

UPD:現時点では、値コンバーターの状態を静的フィールドに保存しています

4

2 に答える 2

1

バインディングを1回に設定してみましたか?

于 2011-02-10T11:51:17.723 に答える
1

コンバーターが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();
    }
}
于 2011-02-10T12:10:25.740 に答える