我有一個C#自定義WPF控件。我有基於DependencyProperty的控件上的屬性。現在WPF控件和自定義控件上的DependencyProperty
public static readonly DependencyProperty CurrentStateProperty =
DependencyProperty.Register("CurrentState", typeof(ControlStateEnum),
typeof(MyCustomControl), new PropertyMetadata(ControlStateEnum.Started));
public ControlStateEnum CurrentState
{
get { return (ControlStateEnum) GetValue(CurrentStateProperty); }
set { SetValue(CurrentStateProperty, value); }
}
,如果我使用的控制,並嘗試使用它,鼻翼:
<myControls:MyCustomControl CurrentState="Loaded" />
在currentState永遠不會被設置爲「裝載」,仍然是「已啓動」。我想讓它具有約束力,但也能夠無約束地設置...有什麼我不明白或缺少的東西?
當我在setter上設置斷點時,它不會在窗口加載時更新。
[Setters not be on Dependency Properties?](http://stackoverflow.com/questions/4225373/setters-not-run-on-dependency-properties) – 2012-07-20 19:50:48
因此,最終的價值是存在控件初始化並加載後,從XAML設置控件。所以處理它的方法是在DependencyProperty上添加一個PropertyChangedHandler。感謝您對診斷的幫助,parapura。 – Locke 2012-07-20 20:32:40