Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to chain multiple value converters in 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. Reverse the value within a range (e.g. range is 1 to 100; value in datacontext is 90; user sees value of 10)
  2. 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.

like image 249
Mal Ross Avatar asked Apr 09 '10 12:04

Mal Ross


People also ask

What is ivalue converter in WPF?

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.

What is a Converter in XAML?

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).

What is WPF MultiBinding?

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.

How do you use converters in xamarin?

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.


2 Answers

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> 
like image 118
Town Avatar answered Sep 21 '22 18:09

Town


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. :)

like image 44
Mal Ross Avatar answered Sep 18 '22 18:09

Mal Ross