wpf更改事件前的依赖属性?

gue*_*est 1 c# wpf wpf-controls

我在控件上有一个依赖属性,它是一个自定义类.

现在是否有一个事件在值被更改之前被提升?

我知道在属性已经更改后引发OnPropertyChanged.

之前我需要一些事件,以便我可以取消更改....以保持控件的状态.

我无法将依赖属性设置回旧值,因为这意味着我在控件中丢失了状态.

谢谢!

HCL*_*HCL 6

如果是你的DependencyProperty,你可以使用ValidateValueCallback验证传入的值并拒绝它,如果它不是你想要的.

在以下示例中,仅接受大于0的值:

public int Test {
    get { return (int)GetValue(TestProperty); }
    set { SetValue(TestProperty, value); }
}


public static readonly DependencyProperty TestProperty =
    DependencyProperty.Register("Test", typeof(int), typeof(YourClass), 
    new UIPropertyMetadata(0), delegate(object v) { 
      return ((int)v) > 0; // Here you can check the value set to the dp
    });
Run Code Online (Sandbox Code Playgroud)