Click here to Skip to main content
13,595,081 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 23 Dec 2010
Licenced CPOL

Virtual value converter - Dynamically bind value converters to elements in XAML

, 24 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Illustrates how to dynamically bind to value converters in XAML using normal Binding
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:

1. Create a virtual converter and have object implement IConvertable:

One can declare IConvertable interface that provides a converter:

public interface IConvertable
    IValueConverter Converter {get;}

and a "virtual" converter:

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

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

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:

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.

Happy coding!


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


About the Author

Ashish Kaila
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 or follow it on Twitter @MixModes

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

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180621.3 | Last Updated 24 Dec 2010
Article Copyright 2010 by Ashish Kaila
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid