Overcoming the limitation of having MultiBinding inside another MultiBinding
September 7, 2011 at 8:39 am Leave a comment
Right now, WPF 4.0 is not allowing to have one MultiBinding inside another. If you will try to do that, you will get an exception:
BindingCollection does not support items of type MultiBinding. Only Binding is allowed.
Here the workaround I found (it used from code, but I my case it was exactly what I needed): you can implement a dummy object in the middle, so child multibinding will bind to property on this dummy object, and then make another regular binding from that property to parent multibinding.
Here the code demonstrating the concept:
MultiBinding parentMultiBinding = new MultiBinding(); BindingAdapter adapter = new BindingAdapter (); BindingOperations.SetBinding(adapter, BindingAdapter.ValueProperty, childMultiBinding); Binding adapterBinding = new Binding(); adapterBinding.Source = adapter; adapterBinding.Path = new PropertyPath(BindingAdapter.ValueProperty); parentMultiBinding.Bindings.Add(adapterBinding);
Here example of BindingAdapter class:
private class BindingAdapter : DependencyObject
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(BindingAdapter), new PropertyMetadata(null));
}
Note: if you doing this, you must store the reference to BindingAdapter all the time while you using it, otherwise it will be garbage collected, and the binding will stop working!
Entry filed under: WPF. Tags: MultiBinding.
Trackback this post | Subscribe to the comments via RSS Feed