带属性的 WPF 转换器与 MultiConverter?

发布于 2024-09-06 22:01:15 字数 123 浏览 5 评论 0原文

使用转换器 (IValueConverter) 并传入其他值作为参数 (ConverterParameter) 与使用 MultiConverter (IMultiValueConverter) 并仅传入多个转换器值之间有什么区别?

What is the difference between using a Converter (IValueConverter) and passing in other values as parameters (ConverterParameter) vs using a MultiConverter (IMultiValueConverter) and just passing in multiple converter values?

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

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

发布评论

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

评论(1

痴骨ら 2024-09-13 22:01:15

有两个主要区别。一是 ConverterParameter 不是 Binding,不会监听属性更改,因此如果值发生更改,Binding 不会自动刷新。

另一个区别是 ConverterParameter 是 Convert 和 ConvertBack 的输入,而 MultiBinding 中的所有绑定都是 Convert 的输入和 ConvertBack 的输出。例如,如果要从 DateTime 转换为字符串,则 ConverterParameter 可能是格式字符串,因为这会影响两个方向的转换:

public class DateTimeConverter
    : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((DateTime)value).ToString((string)parameter, null);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DateTime.ParseExact((string)value, (string)parameter, null);
    }
}

另一方面,如果要从两个双精度数转换为 Size,则可以转换回来时想要返回两个双精度数:

public class SizeConverter
    : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return new Size((double)values[0], (double)values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        var size = (Size)value;
        return new object[] { size.Width, size.Height };
    }
}

There are two main differences. One is that ConverterParameter is not a Binding and does not listen for property changes, so the Binding won't refresh automatically if the value changes.

The other difference is that the ConverterParameter is an input to both Convert and ConvertBack, while all of the Bindings in a MultiBinding are inputs to Convert and outputs of ConvertBack. For example, if you are converting from DateTime to string, you might have the ConverterParameter be a format string, since that affects the conversion in both directions:

public class DateTimeConverter
    : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((DateTime)value).ToString((string)parameter, null);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DateTime.ParseExact((string)value, (string)parameter, null);
    }
}

On the other hand, if you want to convert from two doubles to a Size, then you would want to return two doubles when converting back:

public class SizeConverter
    : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return new Size((double)values[0], (double)values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        var size = (Size)value;
        return new object[] { size.Width, size.Height };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文