WPF:DependencyProperty拒绝工作

时间:2009-08-05 13:54:57

标签: wpf data-binding dependency-properties

我正在尝试在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的调试方法,但我的断点甚至没有被触发。

1 个答案:

答案 0 :(得分:1)

DataContext的{​​{1}}是什么?它是如何知道它应该绑定到TextBlock上的属性?

您需要将Window设置为DataContext实例,或者在绑定上设置Window(或SourceRelativeSource)属性。所有这些属性都作为解析ElementName的绑定对象的方法而存在。 Binding是一个后备,如果其他人都没有设置,但我猜你也没有设置。