public double ADouble
{
get { return (double)GetValue(ADoubleProperty); }
set { SetValue(ADoubleProperty, value); }
}
public static readonly DependencyProperty ADoubleProperty =
DependencyProperty.Register("ADouble", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0, PropertyChanged, CoerceValue), ValidateValue);
private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
MessageBox.Show($"Old Value : {args.OldValue}nNew Value : {args.NewValue}");
}
private static object CoerceValue(DependencyObject d, object value)
{
var a = (double)value;
if (a > 100.0)
{
return 100.0;
}
else if (a < -100.0)
{
return -100.0;
}
return (double)value;
}
private static bool ValidateValue(object value)
{
var a = (double)value;
if (a == 1000.1)
{
return false;
}
return false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.ADouble = 1000.1;
}
System.ArgumentException:““1000.1”不是属性“ADouble”的有效值。”
我让ValidateValueCallback直接返回false,结果在InitializeComponent方法执行时就抛出了异常.
ArgumentException: “ADouble”属性的默认值无效,因为 ValidateValueCallback 失败。
New contributor
FaGGG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.