/// <summary> /// 验证输入 /// </summary> /// <returns></returns> private static object CoerceValue(DependencyObject d, object baseValue) { NumericTextBox host = (NumericTextBox)d; double res; if (baseValue is string s) { if (double.TryParse(s, out double value)) { res = host.CheckValue(value); } else { res = host.Value; } } else if (baseValue is double value) { res = host.CheckValue(value); } else { res = host.Value; } if (res.ToString() != host.Text) { host.Text = res.ToString(); //通过value刷新Text后,光标置于最后 host.SelectionStart = host.Text.Length; } return(res); }
private static object MinCoerce(DependencyObject d, object baseValue) { NumericTextBox host = (NumericTextBox)d; return((double)baseValue > host.Max ? host.Max : baseValue); }