DoubleEdit.xaml.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, int decimals, double value)
  11. {
  12. InitializeComponent();
  13. Title = title;
  14. Value = value;
  15. Editor.NumberDecimalDigits = decimals;
  16. Editor.MinValue = min;
  17. Editor.MaxValue = max;
  18. }
  19. public int DecimalPlaces
  20. {
  21. get => Editor.NumberDecimalDigits;
  22. set => Editor.NumberDecimalDigits = value;
  23. }
  24. public double Value
  25. {
  26. get => Editor.Value.HasValue ? Editor.Value.Value : 0;
  27. set => Editor.Value = value;
  28. }
  29. private void Confirm()
  30. {
  31. if (Editor.Value >= Editor.MinValue && Editor.Value <= Editor.MaxValue)
  32. {
  33. DialogResult = true;
  34. Close();
  35. }
  36. else
  37. {
  38. MessageBox.Show($"Value must be in range [{Editor.MinValue}, {Editor.MaxValue}]");
  39. }
  40. }
  41. private void OK_Click(object sender, RoutedEventArgs e)
  42. {
  43. Confirm();
  44. }
  45. private void Cancel_Click(object sender, RoutedEventArgs e)
  46. {
  47. DialogResult = false;
  48. Close();
  49. }
  50. public static bool Execute(string title, double min, double max, int decimals, ref double value)
  51. {
  52. var edit = new DoubleEdit(title, min, max, decimals, value);
  53. if (edit.ShowDialog() == true)
  54. {
  55. value = edit.Value;
  56. return true;
  57. }
  58. return false;
  59. }
  60. private void ThemableWindow_Loaded(object sender, RoutedEventArgs e)
  61. {
  62. Editor.Focus();
  63. }
  64. private void Editor_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  65. {
  66. if(e.Key == System.Windows.Input.Key.Enter)
  67. {
  68. Confirm();
  69. }
  70. }
  71. }
  72. }