开发者

Value Converter. Force WPF to call it only one time

开发者 https://www.devze.com 2023-02-09 22:31 出处:网络
let\'s say I have following code : <ContextMenu IsEnabled=\"{Binding Converter={StaticResource SomeConverterWithSmartLogic}}\">

let's say I have following code :

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

So, I开发者_运维知识库 did not specified any binding information except Converter...Is it possible to force WPF to call it only one time?

UPD : At this moment i'm storing value converter's state in static fields


If your converter should converter one time only you could write your converter to be that way if that does not cause other disturbances, at least that does not require static fields and the like e.g.

[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();
    }
}


Have you tried setting the binding to onetime?

0

精彩评论

暂无评论...
验证码 换一张
取 消