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