Silverlight: How to force binding after setting the DataContext property

房东的猫 提交于 2019-12-24 04:16:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!