对于我的WPF应用程序,我已经基于TextBox创建了一些自定义控件。这些包括NumericTextBox,WatermarkTextBox和ReturnTextBox。
Numeric和Watermark继承自ReturnTextBox,后者继承自TextBox。
当我使用我的任何自定义文本框时,它们的效果非常好。一个问题似乎是NumericTextBox和MaxLength属性。该属性现在被忽略,不起作用。我的任何自定义控件中都没有代码覆盖或搞乱MaxLength属性。
当我在ReturnTextBox上使用MaxLength时,它就像你期望的那样工作:
<ui:ReturnTextBox MaxLength="35" Width="500" Background="LightYellow" Text="{Binding BrRptCorpName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" TabIndex="2" />
但是当我在NumericTextBox上使用该属性时,它会被忽略并且不起作用:
<ui:NumericTextBox MaxLength="9" Background="LightYellow" Text="{Binding BrRptAmt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" TabIndex="1" />
任何人都可以帮我弄清楚为什么MaxLength会停止工作吗?是因为NumericTextBox不直接从TextBox继承吗?我应该覆盖ReturnTextBox中的MaxLength属性,以便Numeric可以使用它吗?
更新代码
ReturnTextBox类:
public class ReturnTextBox : TextBox
{
static ReturnTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ReturnTextBox), new FrameworkPropertyMetadata(typeof(ReturnTextBox)));
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Return)
{
e.Handled = true;
MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
base.OnPreviewKeyDown(e);
}
}
NumericTextBox
public class NumericTextBox : ReturnTextBox
{
#region Base Class Overrides
static NumericTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericTextBox), new FrameworkPropertyMetadata(typeof(NumericTextBox)));
}
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type",
typeof(NumericType), typeof(NumericTextBox), new UIPropertyMetadata (NumericType.Integer));
public static readonly DependencyProperty SelectAllOnGotFocusProperty = DependencyProperty.Register("SelectAllOnGotFocus",
typeof(bool), typeof(NumericTextBox), new PropertyMetadata(false));
[Description("Numeric Type of the TextBox"), Category("Common Properties")]
public NumericType Type
{
get { return (NumericType)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
[Description("Select text on focus"), Category("Common Properties")]
public bool SelectAllOnGotFocus
{
get
{
return (bool)GetValue(SelectAllOnGotFocusProperty);
}
set
{
SetValue(SelectAllOnGotFocusProperty, value);
}
}
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
Text = ValidateValue(Type, Text);
bool isValid = IsSymbolValid(Type, e.Text);
e.Handled = !isValid;
if (isValid)
{
int caret = CaretIndex;
string text = Text;
bool textInserted = false;
int selectionLength = 0;
if (SelectionLength > 0)
{
text = text.Substring(0, SelectionStart) + text.Substring(SelectionStart + SelectionLength);
caret = SelectionStart;
}
if (e.Text == NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
{
while (true)
{
int ind = text.IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator);
if (ind == -1)
break;
text = text.Substring(0, ind) + text.Substring(ind + 1);
if (caret > ind)
caret--;
}
if (caret == 0)
{
text = "0" + text;
caret++;
}
else
{
if (caret == 1 && string.Empty + text[0] == NumberFormatInfo.CurrentInfo.NegativeSign)
{
text = NumberFormatInfo.CurrentInfo.NegativeSign + "0" + text.Substring(1);
caret++;
}
}
if (caret == text.Length)
{
selectionLength = 1;
textInserted = true;
text = text + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + "0";
caret++;
}
}
else if (e.Text == NumberFormatInfo.CurrentInfo.NegativeSign)
{
textInserted = true;
if (Text.Contains(NumberFormatInfo.CurrentInfo.NegativeSign))
{
text = text.Replace(NumberFormatInfo.CurrentInfo.NegativeSign, string.Empty);
if (caret != 0)
caret--;
}
else
{
text = NumberFormatInfo.CurrentInfo.NegativeSign + Text;
caret++;
}
}
if (!textInserted)
{
text = text.Substring(0, caret) + e.Text +
((caret < Text.Length) ? text.Substring(caret) : string.Empty);
caret++;
}
try
{
double val = Convert.ToDouble(text);
double newVal = val;//ValidateLimits(GetMinimumValue(_this), GetMaximumValue(_this), val);
if (val != newVal)
{
text = newVal.ToString();
}
else if (val == 0)
{
if (!text.Contains(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))
text = "0";
}
}
catch
{
text = "0";
}
while (text.Length > 1 && text[0] == '0' && string.Empty + text[1] != NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
{
text = text.Substring(1);
if (caret > 0)
caret--;
}
while (text.Length > 2 && string.Empty + text[0] == NumberFormatInfo.CurrentInfo.NegativeSign && text[1] == '0' && string.Empty + text[2] != NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
{
text = NumberFormatInfo.CurrentInfo.NegativeSign + text.Substring(2);
if (caret > 1)
caret--;
}
if (caret > text.Length)
caret = text.Length;
Text = text;
CaretIndex = caret;
SelectionStart = caret;
SelectionLength = selectionLength;
e.Handled = true;
}
base.OnPreviewTextInput(e);
}
private static string ValidateValue(NumericType type, string value)
{
if (string.IsNullOrEmpty(value))
return string.Empty;
value = value.Trim();
switch (type)
{
case NumericType.Integer:
try
{
Convert.ToInt64(value);
return value;
}
catch
{
}
return string.Empty;
case NumericType.Decimal:
try
{
Convert.ToDouble(value);
return value;
}
catch
{
}
return string.Empty;
}
return value;
}
private static bool IsSymbolValid(NumericType type, string str)
{
switch (type)
{
case NumericType.Decimal:
if (str == NumberFormatInfo.CurrentInfo.NegativeSign ||
str == NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
return true;
break;
case NumericType.Integer:
if (str == NumberFormatInfo.CurrentInfo.NegativeSign)
return true;
break;
case NumericType.Any:
return true;
}
if (type.Equals(NumericType.Integer) || type.Equals(NumericType.Decimal))
{
foreach (char ch in str)
{
if (!Char.IsDigit(ch))
return false;
}
return true;
}
return false;
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (SelectAllOnGotFocus)
SelectAll();
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (!IsKeyboardFocused && SelectAllOnGotFocus)
{
e.Handled = true;
Focus();
}
base.OnPreviewMouseLeftButtonDown(e);
}
正如您所看到的,我从未在这两个控件中的任何一个中覆盖或对MaxLength属性进行任何更改。但是MaxLength与ReturnTextBox一起使用,并且不能与NumericTextBox一起使用。
感谢您的帮助!
答案 0 :(得分:1)
e.Handled = !isValid
和if(isValid) { e.Handled = true }
是您的问题。当你使用PreviewSomeEvent
时,它们就是隧道。这意味着它们从根目录转到导致事件发生的源。在事件上设置Handled
属性意味着不会引发事件路由中的其他处理程序。 MaxLength
是TextBox
上定义的内容,或者至少是比NumericTextBox
更“真实”的内容。
如果用户输入未通过验证,则不会在“隧道深处”引发事件以检查长度:e.Handled = !isValid
。如果它通过了您的验证,则不会因为e.Handled = true
而提出。
也许一个有用的类比可以覆盖方法并调用base
或不调用{{1}}。
有关活动的更多信息,请查看here