Click here to Skip to main content
13,860,999 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 17 Sep 2012
Licenced CPOL

Bindable Converter, Converter Parameter and StringFormat

, 18 Sep 2012
Rate this:
Please Sign up or sign in to vote.
How to work around that Binding's Converter, ConvertParameter and StringFormat cannot be specified as dynamic Bindings.


In this article we develop ways to overcome the limitation that ConverterParameter cannot be specified as dynamic Binding in WPF and Silverlight 5. These solutions are alternatives to the ones described in the article Bindingable Converter Parameter, and other blogs and fora. Features of the solutions presented here are:

  • Not much extra code in helpers
  • Existing converter implementing IValueConverter can be used without modification
  • Works in data templates
  • Supports RelativeSource  
  • No limitation of number of bindings using bindable ConvertParameter per element.
  • Two-way binding supported
  • Support also for binding to Converter, not just the ConverterParameter.
  • Support for binding to StringFormat (NEW IN 2.0)
  • Silverlight 5 support (see last section)


For Bindings in WPF and Windows, converters can be used to customize the appearance. To pass extra information to a converter the ConverterParameter can be used. However, neither the Converter, nor the ConverterParameter can be specified as a Binding, which several developers finds to be a limitation. Some blogs and forum topics recommend to create a converter that derives from DependencyObject. Then, the converter itself can have dependency properties that can be assigned to bindings in XAML. However, this does not work in dynamic contexts and in data templates.

As pointed out in few places, MultiBinding can be a good choice in many cases. In this article we start in this end and develop a few helpers to make it even easier.

Step 1: Use MultiBinding in WPF

As described in some blogs and forums we should first consider to simply use a MultiBinding instead, specifying the converter parameter binding as the second one like this:

     <MultiBinding Converter="{StaticResource WrappedTestValueConverter}" >
        <Binding Path="FirstName" />
        <Binding Path="ConcatSign" />

In this case we also have to change the Converter to implement the IMultiValueConverter interface. This is simplest done by adding an IMultiValueConverter implementation like this to the original converter:

#region IMultiValueConverter Members

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  return Convert(values[0], targetType, values[1], culture);

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   return new object[] { ConvertBack(value, targetTypes[0], parameter, culture) };


The above code calls the original IValueConverter's Convert and ConvertBack methods.

If you do not want or are unable to change the original converter you can use a generic "adapter" IMultiValueConverter that simply delegates conversion to the original IValueConverter implementation. A generic implementation of such adapter is shown below:

public class MultiValueConverterAdapter : IMultiValueConverter
    public IValueConverter Converter { get; set; }

    private object lastParameter;

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        if (Converter == null) return values[0]; // Required for VS design-time
        if (values.Length > 1) lastParameter = values[1]; 
        return Converter.Convert(values[0], targetType, lastParameter, culture);

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        if (Converter == null) return new object[] { value }; // Required for VS design-time

        return new object[] { Converter.ConvertBack(value, targetTypes[0], lastParameter, culture) };

To use the adapter you can simply wrap the original IValueConverter instance declaration in the resource section like this:

<local:MultiValueConverterAdapter x:Key="WrappedTestValueConverter">
 <local:TestValueConverter />

With this solution we do not have to use any custom bindings in the XAML code, just standard MultiBinding which works also in templates and styles. All required changes are performed on the converter side. With the use of the generic adapter we do not alter the original converters at all.

A limitation with this approach is that it does not fully support two-way binding. The problem is that the parameter passed in as the second value to the Convert method will not be available when ConvertBack is called. This is not be a problem if the parameter is not used in the back-conversion. To partly circumvent this problem the parameter from Convert can be stored and reused in ConvertBack as shown in the MultiValueConverterAdapter implementation above. However, this does not work in scenarios in which the same converter instance is used for several XAML elements, for instance in a data templated list. For a solution, please read on.

Step 2: A new markup extension

To solve the problem with shared converter instances causing problems in a two-way scenario, we can create a new simple markup extension. In this way we also get a more understandable XAML code and do the XAML author does not have to wrap the value converter resource declaration as shown above.

The full implementation of the custom markup extension is shown below:

public class ConverterBindableBinding : MarkupExtension
  public Binding Binding { get; set; }
  public IValueConverter Converter { get; set; }
  public Binding ConverterParameterBinding { get; set; }

  public override object ProvideValue(IServiceProvider serviceProvider)
     MultiBinding multiBinding = new MultiBinding();
     MultiValueConverterAdapter adapter = new MultiValueConverterAdapter();
     adapter.Converter = Converter;
     multiBinding.Converter = adapter; 
     return multiBinding.ProvideValue(serviceProvider);

For markup extensions, the ProvideValue is called whenever the extension is to be applied to an XAML element. What we simply do in this method is to programmatically create a new MultiBinding with the original binding and the converter parameter as sub-bindings, and a new fresh MultiValueConverterAdapter instance. As you see this is very similar to what we did in XAML above, but with the difference that we get a new instance of the adapter for each time the binding is used.

We can now replace the MultiBinding XAML code above with this markup extension like this:

    <local:ConverterBindableBinding Binding="{Binding FirstName}"

                                       Converter="{StaticResource TestValueConverter}" 

                                       ConverterParameterBinding="{Binding ConcatSign}" />

As you see we can use the original TestValueConverter without any changes to its source code or the resource definition. Furthermore, with this solution it is much easier to understand that the ConcatSign binding is used as a source for the ConverterParameter, compared to the MultiBinding solution above. So even if you do not have problems with two-way bindings you likely want to use this solution.

There is still a limitation when using this markup extension in a Style context, because then the same instances of MultiBinding and MultiValueConverterAdapter, created in the ProvideValue method, will be used in all places where the style is applied. Therefore, do not use it in a style if you need two-way binding that uses the converter parameter in the ConvertBack implementation.

Bindable Converter

The solution above can now easily be extended to support binding to the Converter also, not just the ConverterParameter. We just add a new property, ConverterBinding, for that in ConverterBindableBinding and pass this into the <code><code>MultiConverterValueAdapter as a third binding. See submitted source code for the details.

Support for StringFormat (NEW in 2.0)

In the new version (2.0) I have added support for StringFormat too, You can even have a dynamic string format using the StringFormatBinding property. For this I created a new BindableBinding class that is used like this:



     <local:BindableBinding Path="FirstName"

                            Converter="{StaticResource TestValueConverter}"

                            ConverterParameterBinding="{Binding ConcatSign}"

                            StringFormatBinding="{Binding CurrentStringFormat}"/>



A Silverlight 5 solution

The above solutions work for WPF. For Silverlight 5 you can use the solution presented in my article "MultiBinding in Silverlight 5". This MultiBinding implementation supports bindable ConverterParameter and Converter out-of-the box. In contrast to the WPF MultiBinding, it also directly supports the use of IValueConverter, so no adapter converter is needed. In this case the XAML code will be something like this:

<TextBox Text="{z:MultiBinding Converter={StaticResource TestValueConverter} 
               ConverterParameter={Binding ConcatSign} Binding1={Binding FirstName} }" /> 

This Silverlight solution can be used in a Style as well, without problems with shared resources when doing two-way binding.


  • September 17, 2012 - First version submitted.
  • September 19, 2014 - Version 2.0 with support for StringFormat submitted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Henrik Jonsson
Software Developer
Sweden Sweden
Henrik Jonsson is a Microsoft Professional Certified Windows Developer (MCPD) that currently works as an IT consultant in Västerås, Sweden.

Henrik has worked in several small and large software development projects in various roles such as architect, developer, CM and tester.

He regularly reads The Code Project articles to keep updated about .NET development and get new ideas. He has contributed with articles presenting some useful libraries for Undo/Redo, Dynamic Linq Sorting and a Silverlight 5 MultiBinding solution.

You may also be interested in...

Comments and Discussions

BugBindableBinding works only using PropertyElement syntax Pin
Joachim Mairböck26-Mar-15 2:21
professionalJoachim Mairböck26-Mar-15 2:21 
AnswerRe: BindableBinding works only using PropertyElement syntax Pin
Henrik Jonsson27-Mar-15 9:12
memberHenrik Jonsson27-Mar-15 9:12 
QuestionQuestion on StringFormat Pin
kam61018-Sep-14 0:03
memberkam61018-Sep-14 0:03 
AnswerRe: Question on StringFormat Pin
Henrik Jonsson18-Sep-14 5:27
memberHenrik Jonsson18-Sep-14 5:27 
GeneralRe: Question on StringFormat Pin
kam61018-Sep-14 23:29
memberkam61018-Sep-14 23:29 
GeneralRe: Question on StringFormat Pin
Henrik Jonsson19-Sep-14 10:59
memberHenrik Jonsson19-Sep-14 10:59 
GeneralMy vote of 5 Pin
CzimerA17-Feb-13 23:43
memberCzimerA17-Feb-13 23:43 
QuestionTwo way multibinding on silverlight datagrid column Pin
Srečo Vengar1-Nov-12 14:03
memberSrečo Vengar1-Nov-12 14:03 
GeneralMy vote of 5 Pin
Kanasz Robert19-Sep-12 5:47
memberKanasz Robert19-Sep-12 5:47 
Questioncode sample file link broken Pin
Tridip Bhattacharjee18-Sep-12 5:28
professionalTridip Bhattacharjee18-Sep-12 5:28 
AnswerRe: code sample file link broken Pin
Henrik Jonsson18-Sep-12 5:46
memberHenrik Jonsson18-Sep-12 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Henrik Jonsson
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid