【发布时间】:2020-04-28 22:12:15
【问题描述】:
所以,我制作了一个只允许数字的自定义文本框:
public partial class IntegerTextBox : TextBox
{
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
Text = new String(Text.Where(c => Char.IsDigit(c)).ToArray());
SelectionStart = Text.Length;
}
}
我面临的问题是当我以这种方式创建它时
IntegerTextBox textBox = new IntegerTextBox() {... };
或者
<u:IntegerTextBox/>
在我的 .xaml 中
它还覆盖了我正在工作的环境(里程碑系统)使用的默认样式。因此,我得到的不是我应该得到的灰色文本框,而是 Windows 默认的白色文本框。
在 Microsoft 文档中,我找到了一个类似的示例和解释,但我似乎真的找不到让它工作的方法:override example
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
// Insert code to do custom painting.
// If you want to completely change the appearance of your control,
// do not call base.OnPaint(pe).
}
据我了解,它说:不要调用
base.OnTextChanged(e);
(在我的情况下)如果您不想更改控件的外观。我尝试删除它并得到相同的结果。
【问题讨论】:
-
能否请您显示您的类的构造函数。你会覆盖那里的默认样式吗?