试图理解这段代码是如何工作的:
创建依赖属性,
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));
然后在表格中使用它,
<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>
然后Button_Click_1
做,
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_simple.YearPublished++;
}
有用。每次按下按钮时,必须从 PropertyMetadata 更改数字 - 从 2000++ 开始,但我也在文本框的表单上看到它。
问:为什么?
如果我没有在主窗体中放置任何更新 TextBlock 的代码,它是自动更新还是有一些隐藏的机制?或者也许我不完全理解它是如何工作的。或者,如果它的属性有功能,可以更新表单上的数字。