问题
Is it possible to set the DataContext property of a usercontrol after the user control has been loaded, and force the usercontrol to rebind?
回答1:
I'm pretty sure that if you just set the datacontext again, it will rebind
回答2:
If you need to do extra work when the DataContext changes you can use a custom DependencyProperty and bind it to the DataContext property. Use the DependencyPropertyChangedEventHandler to know when the DP changed.
For a more complete explanation see my blog post at http://msmvps.com/blogs/theproblemsolver/archive/2008/12/29/how-to-know-when-the-datacontext-changed-in-your-control.aspx.
回答3:
Setting the DataContext property should cause the rebind.
回答4:
Another way to force re-binding, for example:
MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty,
MyDataGrid.GetBindingExpression(DataGrid.ItemsSourceProperty).ParentBinding);
This method could be used for DataContext as well.
来源:https://stackoverflow.com/questions/369873/silverlight-how-to-force-binding-after-setting-the-datacontext-property