DoubleBox.cs 852 B

1234567891011121314151617181920212223242526272829303132
  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 DoubleBox : NumericUpDown
  12. {
  13. protected override Type StyleKeyOverride => typeof(NumericUpDown);
  14. public DoubleBox()
  15. {
  16. ParsingNumberStyle = NumberStyles.Number;
  17. ShowButtonSpinner = false;
  18. AddHandler(TextInputEvent, TunnelTextEvent, RoutingStrategies.Direct | RoutingStrategies.Tunnel);
  19. }
  20. private void TunnelTextEvent(object? sender, TextInputEventArgs e)
  21. {
  22. e.Text = new string(e.Text?.Where(c => Char.IsDigit(c) || c == '.').ToArray());
  23. if(e.Text == "")
  24. {
  25. e.Handled = true;
  26. }
  27. }
  28. }