Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update multibinding manually

I had a problem with the Binding. The Rectangle.Fill dependency property was bound to an ObservableCollection with the converter. Although the ObservableCollection implements INotifyCollectionChanged, the binding was not updated. I managed, however, to solve this by attaching my delegation to the collection's change notification event and refreshing the binding manually:

    void ColorsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        BindingExpression b = colorRectangle.GetBindingExpression(Rectangle.FillProperty);
        if (b != null)
            b.UpdateTarget();
    }

Lately, however, I changed the Binding to MultiBinding, and the above solution stopped working (the b is null). Is there a way to force the Multibinding to update the target property?

Best regards -- Spook.

like image 711
Spook Avatar asked Apr 07 '11 08:04

Spook


1 Answers

For a multibinding, the binding expression is a MultiBindingExpression, which inherits from BindingExpressionBase, but not from BindingExpression. So GetBindingExpression returns null for a multibinding. Instead you can use BindingOperations.GetMultiBindingExpression:

MultiBindingExpression b = BindingOperations.GetMultiBindingExpression(colorRectangle, Rectangle.FillProperty);
like image 134
Thomas Levesque Avatar answered Sep 21 '22 00:09

Thomas Levesque