TextBoxNumericFinisherBehavior.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Controls;
  7. using System.Windows.Interactivity;
  8. namespace PixiEditor.Helpers.Behaviours
  9. {
  10. class TextBoxNumericFinisherBehavior : Behavior<TextBox>
  11. {
  12. private string _oldText; //Value of textbox before editing
  13. private bool _valueConverted = false; //This bool is used to avoid double convertion if enter is hitted
  14. protected override void OnAttached()
  15. {
  16. base.OnAttached();
  17. AssociatedObject.LostKeyboardFocus += AssociatedObject_LostKeyboardFocus;
  18. AssociatedObject.GotKeyboardFocus += AssociatedObject_GotKeyboardFocus;
  19. AssociatedObject.KeyUp += AssociatedObject_KeyUp;
  20. AssociatedObject.GotMouseCapture += AssociatedObject_GotMouseCapture;
  21. }
  22. private void AssociatedObject_GotMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
  23. {
  24. AssociatedObject.SelectAll(); //Selects all text on mouse click
  25. }
  26. //Converts number to proper format if enter is clicked and moves focus to next object
  27. private void AssociatedObject_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  28. {
  29. if (e.Key != System.Windows.Input.Key.Enter) return;
  30. ConvertValue();
  31. AssociatedObject.MoveFocus(new System.Windows.Input.TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next));
  32. }
  33. private void AssociatedObject_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
  34. {
  35. _valueConverted = false;
  36. _oldText = AssociatedObject.Text; //Sets old value when keyboard is focused on object
  37. }
  38. private void AssociatedObject_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
  39. {
  40. ConvertValue();
  41. }
  42. /// <summary>
  43. /// Converts number from textbox to format "number px" ex. "15 px"
  44. /// </summary>
  45. private void ConvertValue()
  46. {
  47. if (_valueConverted == true) return;
  48. if (int.TryParse(AssociatedObject.Text, out int result) == true)
  49. {
  50. AssociatedObject.Text = string.Format("{0} {1}", AssociatedObject.Text, "px");
  51. }
  52. else //If text in textbox isn't number, set it to old value
  53. {
  54. AssociatedObject.Text = _oldText;
  55. }
  56. _valueConverted = true;
  57. }
  58. protected override void OnDetaching()
  59. {
  60. base.OnDetaching();
  61. AssociatedObject.LostKeyboardFocus -= AssociatedObject_LostKeyboardFocus;
  62. AssociatedObject.GotKeyboardFocus -= AssociatedObject_GotKeyboardFocus;
  63. AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
  64. AssociatedObject.GotMouseCapture -= AssociatedObject_GotMouseCapture;
  65. }
  66. }
  67. }