我是新來的wpf,正如標題所示。我使用的是wpf,因爲它是winforms(無論如何,所有這些都是無意義的綁定),當然,我嘗試了它並被吹走了。Wpf binding澄清(初學者)
所以我在深入挖掘用戶控件和依賴屬性。我讀到,爲了讓ui與引擎蓋下的內容保持同步,您需要使用可觀察的集合,notifypropertychanged/changing和依賴項屬性。
我的問題是這樣的:
比方說,我有這樣的DEP。支柱。對於用戶控件(Media.Color類型):
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
xaml使用它進行綁定,它工作,一切都很好。但是,當它得到更新時,我想用代碼做一些事情。
所以我試圖把一個Console.writeline( 「炒魷魚」),像這樣:
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set {
Console.WriteLine("Fired");
SetValue(ColorProperty, value);
}
}
沒有骰子。有人能告訴我這些東西是如何工作的嗎?我顯然錯過了一些東西(就在前一天,有人告訴我有關MouseCapture的...)。
謝謝你的時間。
編輯
http://www.wpftutorial.net/DependencyProperties.html
基本上它說,在大粗體字母,
重要提示:不要任何邏輯添加到這些屬性,因爲它們 只叫你的時候從代碼中設置屬性。如果您從XAML設置 屬性,則直接調用SetValue()方法。
如果您使用的是Visual Studio,則可以鍵入propdp,然後按兩次製表符以 創建依賴項屬性。
然後繼續解釋爲什麼以及如何繼續。
解決方案
所以,我試了一下@Krishna建議,和我的用戶控制墜毀並燃燒。
這是我的道具。 (就像之前提出這個問題一樣)。
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerMaster), new PropertyMetadata(default(Color)));
原來,問題是用 (...)新Prop.Metadata(NULL,OnPropChanged)
使用
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerMaster), new PropertyMetadata(OnColorChanged));
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine(e.NewValue);
}
產生一個漂亮的勝利。
謝謝你的時間和答案。
的可能重複[聽依賴屬性的變化(http://stackoverflow.com/questions/4764916/listen-to-changes-of-dependency-property) – 2014-09-29 11:13:02