我正试图手动将数据重新绑定到ContentControl。手动将数据重新绑定到WPF ContentControl
<UserControl.Resources>
<local:MyModel x:Key="myModel" />
<DataTemplate DataType="{x:Type local:MyModel}">
<StackPanel>
<TextBlock Text="{Binding Path=Property1}"></TextBlock>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl Content="{Binding Source={StaticResource myModel}}" Name="myView">
</ContentControl>
</Grid>
而且在后面
MyModel myModel = this.FindResource("myModel") as MyModel ;
myModel.Property1 = "Test";
var bindingExpression = myView.GetBindingExpression(ContentControl.ContentProperty);
bindingExpression.UpdateTarget();
但它似乎没有工作的代码,我缺少什么?
模型是否实现INotifyPropertyChanged?调试输出是否给出了任何绑定错误? – 2011-12-19 14:47:16
除了使用'cntView'命名你的ContentControl被命名为'myview'和你的代码隐藏之外,我不得不猜测它与绑定到StaticResource有关。静态通常意味着它不会改变,所以WPF绑定一次并忘记它。 – Rachel 2011-12-19 14:48:59
@Erno:没有模型没有实现INotifyPropertyChanged,但这就是为什么我试图手动重新绑定数据。另外我没有看到任何错误。 – exoz 2011-12-19 15:09:17