2009-08-05 95 views
1

我想在窗口上註冊3個依賴項屬性來控制它的格式。我一遍又一遍地查看代碼,但我一定錯過了一些東西。WPF:DependencyProperty拒絕工作

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

什麼是TextBlockDataContext?它如何知道它應該綁定到您的Window上的屬性?

您需要任何設置DataContextWindow實例,或者在你的綁定設置Source(或RelativeSource,或者ElementName)性能。所有這些屬性都是作爲解決您的Binding的綁定對象的手段而存在的。 DataContext是一個後備,如果其他都沒有設置,但我猜你還沒有設置。

+0

我的印象是最接近的頂級水平。開發ASP.NET已經污染了我的腦海;) 通過在父容器上設置RelativeSource解決了問題。謝謝。 – Echilon 2009-08-05 14:03:50