C# Silverlight:如何在设置 DataContext 属性后强制绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/369873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:48:56  来源:igfitidea点击:

Silverlight: How to force binding after setting the DataContext property

c#.netsilverlightdata-binding

提问by Jeremy

Is it possible to set the DataContext property of a usercontrol after the user control has been loaded, and force the usercontrol to rebind?

是否可以在加载用户控件后设置用户控件的 DataContext 属性,并强制用户控件重新绑定?

采纳答案by Jacob Adams

I'm pretty sure that if you just set the datacontext again, it will rebind

我很确定,如果您再次设置数据上下文,它将重新绑定

回答by Anderson Imes

Setting the DataContext property should cause the rebind.

设置 DataContext 属性应该会导致重新绑定。

回答by Maurice

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.

如果您需要在 DataContext 更改时做额外的工作,您可以使用自定义 DependencyProperty 并将其绑定到 DataContext 属性。使用 DependencyPropertyChangedEventHandler 了解 DP 何时更改。

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.

有关更完整的解释,请参阅我在http://msmvps.com/blogs/theproblemsolver/archive/2008/12/29/how-to-know-when-the-datacontext-changed-in-your-control 上的博客文章。 ASPX

回答by Jon Kelling

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.

此方法也可用于 DataContext。