WPF:DependencyPropertyが機能しない
-
22-07-2019 - |
質問
ウィンドウのフォーマットを制御するために、ウィンドウに3つの依存関係プロパティを登録しようとしています。私はコードを何度も見てきましたが、何かを見逃しているに違いありません。
public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Color), typeof(WinStickyFingers), new PropertyMetadata(Colors.White));
public Color TextColor {
get { return (Color)base.GetValue(TextColorProperty); }
set { base.SetValue(TextColorProperty, value); }
}
public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(WinStickyFingers), new PropertyMetadata(Colors.Black));
public Color BackgroundColor {
get { return (Color)base.GetValue(BackgroundColorProperty); }
set {
base.SetValue(BackgroundColorProperty, value);
}
}
<TextBlock DockPanel.Dock="Top" Text="{Binding Name}" Foreground="{Binding TextColor,Converter={StaticResource DebugConverter}}" Background="{Binding Path=BackgroundColor}" />
Bea Stollnitzのデバッグ方法を使用していますが、ブレークポイントがトリガーされません。
解決
TextBlock
の DataContext
とは何ですか? Window
のプロパティにバインドすることになっていることをどのようにして知るのですか?
DataContext
を Window
インスタンスに設定するか、 Source
(または RelativeSource
を設定する必要があります。または ElementName
)バインディングのプロパティ。これらのプロパティはすべて、 Binding
のバインドされたオブジェクトを解決する手段として存在します。 DataContext
は、他のどれも設定されていない場合のフォールバックですが、どちらも設定していないと思います。
所属していません StackOverflow