IntegerBox.cs 990 B

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