DoubleBox.cs 976 B

1234567891011121314151617181920212223242526272829303132333435
  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 DoubleBox : NumericUpDown
  13. {
  14. protected override Type StyleKeyOverride => typeof(NumericUpDown);
  15. public DoubleBox()
  16. {
  17. ParsingNumberStyle = NumberStyles.Number;
  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?.WithIndex()
  24. .Where(x => (x.Key == 0 && x.Value == '-') || Char.IsDigit(x.Value) || x.Value == '.')
  25. .Select(x => x.Value).ToArray());
  26. if(e.Text == "")
  27. {
  28. e.Handled = true;
  29. }
  30. }
  31. }