值转换器。强制 WPF 仅调用一次

发布于 2024-10-17 04:34:26 字数 232 浏览 2 评论 0原文

假设我有以下代码:

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

那么,除了 Converter 之外,我没有指定任何绑定信息...是否可以强制 WPF 仅调用它一次?

UPD:此时我将值转换器的状态存储在静态字段中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2024-10-24 04:34:26

如果您的转换器应该只转换一次,您可以将转换器编写为这种方式,如果这不会引起其他干扰,至少不需要静态场等,例如

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

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();
    }
}
朦胧时间 2024-10-24 04:34:26

您是否尝试过将绑定设置为一次性?

Have you tried setting the binding to onetime?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文