Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C# 4.0

Silverlight Multi-Binding

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
17 Nov 2011CPOL4 min read 30.4K   435   11  
Performing multi-binding in Silverlight.
using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Data;
using System.Windows.Markup;
using System.Reflection;

namespace slMultiBinding
{

    [ContentProperty("Bindings")]
    public class MyMultiBindingExtension : MarkupExtension
    {

        private List<Binding> _Bindings = new List<Binding>();
        public List<Binding> Bindings
        {
            get { return _Bindings; }
        }

        private IMultiValueConverter _Converter;
        public IMultiValueConverter Converter
        {
            get { return _Converter; }
            set { _Converter = value; }
        }

        /// <summary>
        /// return result as Binding to attached property rather than regular value!
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {
                IProvideValueTarget pvt = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;                
                DependencyObject  TargetObject = pvt.TargetObject as DependencyObject ;

                for (int i = 0; i < _Bindings.Count ; i++)
                {
                    string sDpName="Binding"+(i+1).ToString()+"Property";
                    //using reflection to get the DependencyProperty from type/string
                    FieldInfo fi = typeof(MultiBindingUtil).GetField(sDpName);
                    if (fi==null)
                    {
                        throw new IndexOutOfRangeException("MultiBindingUtil Max number of binding(3) exceeded");
                    }
                    DependencyProperty dp = (DependencyProperty)fi.GetValue(null);
                    BindingExpressionBase beb = BindingOperations.SetBinding(TargetObject, dp, _Bindings[i]);                   
                }
                TargetObject.SetValue(MultiBindingUtil.ConverterProperty, this.Converter);

                Binding b = new Binding();
                b.Source = TargetObject;
                b.Path =new PropertyPath(  MultiBindingUtil.MBResultProperty);
                return b;
            }
            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