简体   繁体   English

为什么ValidateValueCallback被调用两次?

[英]Why is ValidateValueCallback called twice?

Suppose I've the following DependencyObject type, 假设我具有以下DependencyObject类型,

public class Test : DependencyObject
{


    public int Order
    {
        get { return (int)GetValue(OrderProperty); }
        set { SetValue(OrderProperty, value); }
    }

    public static readonly DependencyProperty OrderProperty =
        DependencyProperty.Register("OrderProperty",
        typeof(int),
        typeof(Test),
        new FrameworkPropertyMetadata(6,
            new PropertyChangedCallback(OnOrderPropertyChanged),
            new CoerceValueCallback(OnCoerceValueCallBack)),
        new ValidateValueCallback(OnValidateValueCallBack));

    static void OnOrderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Console.WriteLine("OnOrderPropertyChanged");
    }

    static object OnCoerceValueCallBack(DependencyObject d, object baseValue)
    {
        Console.WriteLine("OnCoerceValueCallBack");
       return 200;
    }

    static bool OnValidateValueCallBack(object value)
    {
        Console.WriteLine("OnValidateValueCallBack");
        int iValue = (int)value;
        return iValue > 5;
    }
}

When I create an instance of Test , I see OnValidateValueCallBack is called twice and OnCoerceValueCallBack isn't called at all. 当创建Test的实例时,我看到OnValidateValueCallBack被调用了两次,而OnCoerceValueCallBack根本没有被调用。 Based on what I saw, I guess when I create the instance, WPF will call OnValidateValueCallBack to check whether the default value is valid, if so, it will use the default value and won't call CoerceValueCallback at all, so where does the second call of OnValidateValueCallBack come from? 根据我所看到的,我猜想在创建实例时,WPF将调用OnValidateValueCallBack来检查默认值是否有效,如果是,它将使用默认值并且根本不会调用CoerceValueCallback ,那么第二个在哪里调用OnValidateValueCallBack来自OnValidateValueCallBack

I can confirm this behavior. 我可以确认这种行为。 Upon registering a dependency property, the validate callback is called by the internals of WPF. 注册依赖项属性后,WPF内部将调用validate回调。 One call always occurs , regardless of the metadata passed. 不管传递的元数据如何, 总会发生一次调用 A second call only occurs when a default value is passed with the metadata. 仅当元数据传递默认值时,才会发生第二次调用

Originally I thought the first call was due to internal initialization of WPF, so it would always be the default value of the type. 最初,我认为第一次调用是由于WPF的内部初始化,所以它始终是该类型的默认值。 This is not the case! 不是这种情况! The object passed in the first call is either the default value of the type, or the default value which is specified in the metadata . 第一次调用中传递的对象可以是类型的默认值,也可以是元数据中指定的默认值 The second call seems more logical, and just passes the default value. 第二个调用似乎更合乎逻辑,并且仅传递默认值。

No coerce or changed callbacks are called upon registering. 注册时不会调用强制或更改的回调。

What the use of the first call is is a mystery to me. 第一次拨打电话的用途对我来说是个谜。

Relevant (unanswered) question on msdn. 有关 msdn的相关问题(未答复)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM