问题描述
我有一个情况,我需要显示一个整数值,绑定到我的数据上下文的一个属性,通过两个单独的转换:
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到100;数据文本中的值为90;用户看到值为10)
- 将数字转换为字符串
我意识到我可以通过创建自己的转换器(实现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. :)
谢谢。
推荐答案
由Josh Smith提供: 。
Found exactly what I was looking for, courtesy of Josh Smith: Piping Value Converters (archive.org link).
他定义了一个 ValueConverterGroup
类,其在XAML中的使用正是我所希望的。以下是一个示例:
He defines a ValueConverterGroup
class, whose use in XAML is exactly as I was hoping for. Here's an example:
<!-- Converts the Status attribute text to a SolidColorBrush used to draw
the output of statusDisplayNameGroup. -->
<local:ValueConverterGroup x:Key="statusForegroundGroup">
<local:IntegerStringToProcessingStateConverter />
<local:ProcessingStateToColorConverter />
<local:ColorToSolidColorBrushConverter />
</local:ValueConverterGroup>
很棒的东西。谢谢,乔希。 :)
Great stuff. Thanks, Josh. :)
这篇关于有没有办法链接XAML中的多个价值转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!