代码之家  ›  专栏  ›  技术社区  ›  Echilon

WPF:DependencyProperty拒绝工作

  •  1
  • Echilon  · 技术社区  · 15 年前

    我试图在一个窗口上注册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 回复  |  直到 13 年前
        1
  •  1
  •   Tim Cooper    13 年前

    什么是 DataContext TextBlock ?它如何知道它应该绑定到 Window ?

    你需要两套 数据上下文 窗口 实例,或设置 Source (或) RelativeSource ElementName )绑定上的属性。所有这些属性都作为解析绑定对象的方法存在。 Binding . 数据上下文 如果没有其他设置,则是一种回退,但我猜您也没有设置。