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:
- Reverse the value within a range (e.g. range is 1 to 100; value in datacontext is 90; user sees value of 10)
- convert the number to a string
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.
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. :)
Thanks.
Found exactly what I was looking for, courtesy of Josh Smith: Piping Value Converters (archive.org link).
He defines a
ValueConverterGroup
class, whose use in XAML is exactly as I was hoping for. Here's an example:Great stuff. Thanks, Josh. :)
Town's implementation of Gareth Evans's Silverlight project is great, however it does not support different converter parameters.
I modified it so you can provide parameters, comma delimited (unless you escape them of course).
Converter:
Note: ConvertBack is not implemented here, see my Gist for the full version.
Implementation:
Yes, there are ways to chain converters but it does not look pretty and you don't need it here. If you ever come to need this, ask yourself is that really the way to go? Simple always works better even if you have to write your own converter.
In your particular case, all you need to do is format a converted value to a string.
StringFormat
property on aBinding
is your friend here.I used this method by Gareth Evans in my Silverlight project.
Here's my implementation of it:
Which can then be used in XAML like this: