我有一个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