1

このコードがどのように機能するかを理解しようとしています:

依存関係プロパティを作成し、

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++ 以降ですが、textBox のフォームでも見ました。

質問: なぜですか?

メイン フォームに TextBlock を更新するためのコードを何も配置しない場合、自動的に更新されますか、それとも隠しメカニズムがありますか? あるいは、それがどのように機能するかを完全に理解していないのかもしれません。または、そのプロパティにフォーム上の数値を更新する機能がある場合もあります。

4

2 に答える 2