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.

Spook
  • 25,318
  • 18
  • 90
  • 167

1 Answers1

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);
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 9
    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. – Tomer W Nov 13 '12 at 10:08