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:
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.
The IValueConverter interface consists of two methods, Convert() and ConvertBack() . Convert method gets called when source updates target object. ConvertBack method gets called when target updates source object.
In this article, I'm going to show you how you can use value converter in XAML to convert one format of data into another format. In our case, we'll convert a string value (value in the textbox) to a Boolean value (checked status of a checkbox).
MultiBinding takes multiple values and combines them into another value. There are two ways to do MultiBinding , either using StringFormat or by a converter. The StringFormat. is simple compared to a converter, so we will start with that first.
You can write value converters to be more generalized and to accept several different types of data. The Convert and ConvertBack methods can use the as or is operators with the value parameter, or can call GetType on that parameter to determine its type, and then do something appropriate.
I used this method by Gareth Evans in my Silverlight project.
Here's my implementation of it:
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 }
Which can then be used in XAML like this:
<c:ValueConverterGroup x:Key="InvertAndVisibilitate"> <c:BooleanInverterConverter/> <c:BooleanToVisibilityConverter/> </c:ValueConverterGroup>
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:
<!-- 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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With