[英]WPF Controls and DependencyProperty on Custom Controls
我有一个C#自定义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); }
}
现在,如果我使用该控件,并尝试使用它,ala:
<myControls:MyCustomControl CurrentState="Loaded" />
CurrentState永远不会设置为“已加载”,而是保持为“已启动”。 我想使其具有绑定能力,但也可以使其不绑定而被设置...是否有我不理解或缺少的东西?
当我在设置器上设置断点时,它不会在窗口加载时更新。
您确定在控件加载后没有在其他地方更改枚举,因为这应该按预期工作
因此,最后在初始化并加载控件后,通过XAML在控件上设置了值。 因此,处理该问题的方法是在DependencyProperty上添加一个PropertyChangedHandler。
parapura有指向正确答案的链接(http://stackoverflow.com/questions/4225373/setters-not-run-on-dependency-properties)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.