Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight:如何在设置DataContext属性后强制绑定_C#_.net_Silverlight_Data Binding - Fatal编程技术网

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

C# Silverlight:如何在设置DataContext属性后强制绑定,c#,.net,silverlight,data-binding,C#,.net,Silverlight,Data Binding,是否可以在加载用户控件后设置usercontrol的DataContext属性,并强制usercontrol重新绑定?我敢肯定,如果您只是再次设置DataContext,它将重新绑定设置DataContext属性将导致重新绑定。如果需要在DataContext更改时进行额外工作,则可以使用自定义DependencyProperty并将其绑定到DataContext属性。使用DependencyPropertyChangedEventHandler了解DP何时更改 有关更完整的解释,请参阅我的博客

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

我敢肯定,如果您只是再次设置DataContext,它将重新绑定

设置DataContext属性将导致重新绑定。

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


有关更完整的解释,请参阅我的博客文章。

强制重新绑定的另一种方法,例如:

MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, 
    MyDataGrid.GetBindingExpression(DataGrid.ItemsSourceProperty).ParentBinding);

此方法也可用于DataContext。

如果将数据上下文设置为与之前相同,则不会重新绑定。在本例中,首先将其设置为null,然后设置为之前的值,以强制其重新绑定。