Tuesday, May 3, 2011

Update all bindings in UserControl at once

I need to update all the bindings on my UserControl when its visibility changes to Visible. Pretty much all my bindings are bound to the DataContext property of the user control so I'm trying to update the target of that binding:

BindingOperations.GetBindingExpressionBase(this, UserControl.DataContextProperty).UpdateTarget();

But I get null as the result of GetBindingExpression(..) method and I'm wondering if I'm using this wrong.

Also, is there any other good way to refresh all bindings on the control (which use DataContext as the source).

From stackoverflow
  • Well, you could just re-assign the DataContext:

    var dataContext = DataContext;
    DataContext = null;
    DataContext = dataContext;
    

    HTH, Kent

    Alexandra : OMG, you are a genius! Why, WHY haven't I thought of this?! Thanks, this worked :)
    BrettRobi : Is there a more graceful WPF'y way of doing this? Shouldn't WPF support some sort of binding refresh mechanism?

0 comments:

Post a Comment