IntegerBox.cs 872 B

123456789101112131415161718192021222324252627282930313233
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Interactivity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace InABox.Avalonia.Components;
  11. public class IntegerBox : NumericUpDown
  12. {
  13. protected override Type StyleKeyOverride => typeof(NumericUpDown);
  14. public IntegerBox()
  15. {
  16. ParsingNumberStyle = NumberStyles.Integer;
  17. FormatString = "N0";
  18. ShowButtonSpinner = false;
  19. AddHandler(TextInputEvent, TunnelTextEvent, RoutingStrategies.Direct | RoutingStrategies.Tunnel);
  20. }
  21. private void TunnelTextEvent(object? sender, TextInputEventArgs e)
  22. {
  23. e.Text = new string(e.Text?.Where(c => Char.IsDigit(c)).ToArray());
  24. if(e.Text == "")
  25. {
  26. e.Handled = true;
  27. }
  28. }
  29. }