有没有办法在 XAML 中链接多个值转换器? [英] Is there a way to chain multiple value converters in XAML?

查看:24
本文介绍了有没有办法在 XAML 中链接多个值转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要显示一个整数值,该值绑定到我的数据上下文中的一个属性,经过两次单独的转换后:

I've got a situation in which I need to show an integer value, bound to a property on my data context, after putting it through two separate conversions:

  1. 反转范围内的值(例如,范围是 1 到 100;数据上下文中的值是 90;用户看到的值为 10)
  2. 将数字转换为字符串

我意识到我可以通过创建自己的转换器(实现 IValueConverter)来完成这两个步骤.但是,我已经有了一个单独的值转换器,它只完成第一步,第二步由 Int32Converter 覆盖.

I realise I could do both steps by creating my own converter (that implements IValueConverter). However, I've already got a separate value converter that does just the first step, and the second step is covered by Int32Converter.

有没有一种方法可以在 XAML 中链接这两个现有类,而无需创建进一步的类来聚合它们?

Is there a way I can chain these two existing classes in XAML without having to create a further class that aggregates them?

如果我需要澄清其中任何一个,请告诉我.:)

If I need to clarify any of this, please let me know. :)

谢谢.

推荐答案

我使用了 这个方法 作者 Gareth Evans 在我的 Silverlight 项目中.

I used this method by Gareth Evans in my Silverlight project.

这是我的实现:

public class ValueConverterGroup : List<IValueConverter>, IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return this.Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

然后可以像这样在 XAML 中使用它:

Which can then be used in XAML like this:

<c:ValueConverterGroup x:Key="InvertAndVisibilitate">
   <c:BooleanInverterConverter/>
   <c:BooleanToVisibilityConverter/>
</c:ValueConverterGroup>

这篇关于有没有办法在 XAML 中链接多个值转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆