Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / WPF

Bindable Converter Parameter

Rate me:
Please Sign up or sign in to vote.
4.81/5 (8 votes)
2 Jul 2013CPOL7 min read 120K   4.5K   28  
A simple technique to achieve Bindable-ConverterParameter in WPF's XAML.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Windows.Data;
using System.Windows;

namespace BindableConverterParameter
{
    class MyBindedParameterMultiBinding : MarkupExtension
    {
        public Binding Binding1 { get; set; }
        public Binding Binding2 { get; set; }
        public Binding ConverterParameter { get; set; }
        public IMultiValueConverter Converter { get; set; }


        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {
                IProvideValueTarget pvt = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                DependencyObject TargetObject = pvt.TargetObject as DependencyObject;
                BindingOperations.SetBinding(TargetObject, CustomBindingUtil.Binding1Property, Binding1);
                BindingOperations.SetBinding(TargetObject, CustomBindingUtil.Binding2Property, Binding2);
                BindingOperations.SetBinding(TargetObject, CustomBindingUtil.ConverterParameterProperty, ConverterParameter);
                TargetObject.SetValue(CustomBindingUtil.MultiConverterProperty, this.Converter);

                Binding b = new Binding();
                b.Source = TargetObject;
                b.Path = new PropertyPath(CustomBindingUtil.MBResultProperty);
                return b.ProvideValue(serviceProvider);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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) self employed
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions