依存関係オブジェクトとプロパティの使用方法を学びたいです。私はこのクラスを作成しました、
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty =
DependencyProperty.Register("TestDateTime",
typeof(DateTime),
typeof(TestDependency),
new PropertyMetadata(DateTime.Now));
public DateTime TestDateTime
{
get { return (DateTime) GetValue(TestDateTimeProperty); }
set { SetValue(TestDateTimeProperty, value); }
}
}
ウィンドウクラスはこんな感じ
public partial class MainWindow : Window
{
private TestDependency td;
public MainWindow()
{
InitializeComponent();
td = new TestDependency();
td.TestDateTime = DateTime.Now;
}
}
これを使用して、これをグリッドに追加することにより、TextBlock内の現在のDateTimeを表示します。これは毎秒更新されます。
<Grid>
<TextBlock Text="{Binding TestDateTime,ElementName=td}" Width="200" Height="200"/>
</Grid>
TextBlockは表示されますが、日時の値がまったく含まれていません。私は何が間違っているのですか?