0

我有一个 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 永远不会设置为“已加载”并保持“已启动”。我想让它能够绑定,但也能够在没有绑定的情况下设置......有什么我不明白或遗漏的吗?

当我在设置器上设置断点时,它不会在窗口加载时更新。

4

2 回答 2

1

您确定在控件加载后没有在其他地方更改枚举,因为这应该按预期工作

于 2012-07-20T20:14:57.803 回答
0

因此,最终在控件初始化和加载后,从 XAML 为控件设置了值。所以处理它的方法是在DependencyProperty上添加一个PropertyChangedHandler。

parapura 有一个指向正确答案的链接(http://stackoverflow.com/questions/4225373/setters-not-run-on-dependency-properties)。

于 2012-09-06T18:12:56.797 回答