Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / XAML

Dynamically Bind Value Converters to Elements in XAML

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Dec 2010CPOL 17.3K   1   1
How to dynamically bind value converters to elements in XAML

Recently, I faced a scenario where I wanted to create a generic control template in WPF for a user control that refers to value converters dynamically in order to render parts of the control.

Depending on the enumeration passed as the parameter, converter is supposed to render different aspect of the object. The actual formatter is unknown at the base control level except for the enumeration.

Concrete implementation/usage of the control should specify the converter as its property. I tried doing this in XAML:

SomeProperty = {Binding Converter={Binding MyConverterProperty} }

The above will result in runtime exception as Converter property is not DependencyProperty within BindingMarkupExtension.

There are three ways of achieving the above:

One can declare IConvertable interface that provides a converter:

C#
public interface IConvertable   
{    
    IValueConverter Converter {get;}    
}

and a "virtual" converter:

C#
public class VirtualConverter : IValueConverter   
{ 
    public void Convert(object value, ....)   
    {    
        IValueConverter converter;    
        if ((value is IConvertible) && ((converter = (value as IConvertible) != null)))    
        {    
            converter.Convert(....);    
        }    
        throw new InvalidOperationException();    
    }    
    :    
    :    
}

And now you can create a static resource of type VirtualConverter and use Binding normally specifying VirtualConverter as the converter.

  1. Create a virtual converter and have object implement IConvertable:
  2. Implement IConvertible but instead of creating Virtual converter, declare a markup extension that takes in IConvertible and returns IValueConverter. Then your markup will look like:
    C#
    SomeProperty = "{Binding Converter={ConverterExtension Source={Binding}}}"
  3. Use attached property to define Converter on an object rather than implementing IConvertible and specify it as converter in binding.

License

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


Written By
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.

Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes

In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net

Comments and Discussions

 
QuestionSamples Pin
Shai`tan30-Nov-18 9:59
Shai`tan30-Nov-18 9:59 

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.