DoubleEdit.xaml.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using InABox.Wpf;
  2. using System.Windows;
  3. namespace InABox.WPF
  4. {
  5. /// <summary>
  6. /// Interaction logic for NumberEdit.xaml
  7. /// </summary>
  8. public partial class DoubleEdit : ThemableWindow
  9. {
  10. public DoubleEdit(string title, double min, double max, double value)
  11. {
  12. InitializeComponent();
  13. Title = title;
  14. Value = value;
  15. Editor.MinValue = min;
  16. Editor.MaxValue = max;
  17. }
  18. public double Value
  19. {
  20. get => Editor.Value.HasValue ? Editor.Value.Value : 0;
  21. set => Editor.Value = value;
  22. }
  23. private void Confirm()
  24. {
  25. if (Editor.Value >= Editor.MinValue && Editor.Value <= Editor.MaxValue)
  26. {
  27. DialogResult = true;
  28. Close();
  29. }
  30. else
  31. {
  32. MessageBox.Show($"Value must be in range [{Editor.MinValue}, {Editor.MaxValue}]");
  33. }
  34. }
  35. private void OK_Click(object sender, RoutedEventArgs e)
  36. {
  37. Confirm();
  38. }
  39. private void Cancel_Click(object sender, RoutedEventArgs e)
  40. {
  41. DialogResult = false;
  42. Close();
  43. }
  44. public static bool Execute(string title, double min, double max, ref double value)
  45. {
  46. var edit = new DoubleEdit(title, min, max, value);
  47. if (edit.ShowDialog() == true)
  48. {
  49. value = edit.Value;
  50. return true;
  51. }
  52. return false;
  53. }
  54. private void ThemableWindow_Loaded(object sender, RoutedEventArgs e)
  55. {
  56. Editor.Focus();
  57. }
  58. private void Editor_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  59. {
  60. if(e.Key == System.Windows.Input.Key.Enter)
  61. {
  62. Confirm();
  63. }
  64. }
  65. }
  66. }