So, I made a Custom TextBox, that would only allow numbers:
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;
}
}
The problem I'm facing is that when I create it ether this way
IntegerTextBox textBox = new IntegerTextBox() {... };
Or
<u:IntegerTextBox/>
in my .xaml
It also overrides the default style used by the environment that I'm working at (Milestone Systems). So instead of getting grey TextBoxes that I should get, I get Windows Default white TextBox.
In the Microsoft documentation, I found a similar example with the explanation, but I can't really seem to find a way to make it work: 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).
}
As far as I understand, it says: don't call the
base.OnTextChanged(e);
(in my case) if you don't want to change the appearance of the control. I tried removing it and got the same results.
Define an implicit
Stylefor yourIntegerTextBoxinApp.xaml:Then it should inherit any implicit
Stylethat is currently applied to<TextBox />elements.