我正在尝试在Window上注册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的调试方法,但我的断点甚至没有被触发。
答案 0 :(得分:1)
DataContext
的{{1}}是什么?它是如何知道它应该绑定到TextBlock
上的属性?
您需要将Window
设置为DataContext
实例,或者在绑定上设置Window
(或Source
或RelativeSource
)属性。所有这些属性都作为解析ElementName
的绑定对象的方法而存在。 Binding
是一个后备,如果其他人都没有设置,但我猜你也没有设置。