Trying to understand how this code works:
Create dependency property,
public int YearPublished
{
get { return (int)GetValue(YearPublishedProperty); }
set { SetValue(YearPublishedProperty, value); }
}
public static readonly DependencyProperty YearPublishedProperty =
DependencyProperty.Register(
"YearPublished",
typeof(int),
typeof(SimpleControl),
new PropertyMetadata(2000));
Then use it in a form,
<xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:SimpleControl x:Name="_simple" />
<TextBlock Text="{Binding YearPublished, ElementName=_simple}"
FontSize="30"
TextAlignment="Center" />
<Button Content="Change Value"
FontSize="20"
Click="Button_Click_1"/>
</StackPanel>
Then for Button_Click_1
do,
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_simple.YearPublished++;
}
It works. Each time when you press the button, number must be changed from PropertyMetadata - from 2000++, but also I saw it on form in textBox.
Question: Why?
If I don't put any code for updating TextBlock in main Form, is it automatically updating or is there some hidden mechanism for it? Or maybe I do not fully understand how it works. Or maybe if its property there are features, that update the number on the form.