Suppose I've the following DependencyObject type,
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 instance of Test, I saw OnValidateValueCallBack was called twice and OnCoerceValueCallBack wasn't called at all. Based on what I saw, I guess when create the instance, WPF will call OnValidateValueCallBack to check whether the default value is validate, if so, it will use the default value and didn't call CoerceValueCallback at all, so where the second call of OnValidateValueCallBack come from?