How to update multibinding manually
Asked Answered
M

1

15

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.

Matey answered 7/4, 2011 at 8:9 Comment(1)
Did you ever figure out why the Binding/Multibinding is not responding to the CollectionChanged events?Future
A
27

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);
Admiration answered 7/4, 2011 at 8:17 Comment(1)
mostly getBinding is used for Manual Source Update... and for that you can use the generalized: BindingExpressionBase be = BindingOperations.GetBindingExpressionBase(colorRectangle, Rectangle.FillProperty); this way it doesn't matter if it is Binding, MultiBinding or any other that will come later.Culmiferous

© 2022 - 2024 — McMap. All rights reserved.