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!

Advertisement

Entry filed under: WPF. Tags: .

Working with “resx” resources Watermark TextBox Behavior (Silverlight 4)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories


Follow

Get every new post delivered to your Inbox.