I am trying to manually rebind the data to a 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>
And in the code behind
MyModel myModel = this.FindResource("myModel") as MyModel ;
myModel.Property1 = "Test";
var bindingExpression = myView.GetBindingExpression(ContentControl.ContentProperty);
bindingExpression.UpdateTarget();
But it doesn’t seem to work, what am I missing?
If
Property1doesn’t raise a change event when it changes, the TextBox doesn’t know that the data has changed and to update.Make your
MyModelclass implementINotifyPropertyChanged, and raise thePropertyChangedevent whenProperty1changes, and it will work.Also, you don’t need to explicitly tell WPF that
ContentControl.Contenthas changed because it hasn’t. It still points to the sameMyModelobject.