EditableTextBlock.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using PixiEditor.Models.Controllers;
  5. using PixiEditor.Models.Controllers.Shortcuts;
  6. namespace PixiEditor.Views
  7. {
  8. /// <summary>
  9. /// Interaction logic for EditableTextBlock.xaml
  10. /// </summary>
  11. public partial class EditableTextBlock : UserControl
  12. {
  13. // Using a DependencyProperty as the backing store for TextBlockVisibility. This enables animation, styling, binding, etc...
  14. public static readonly DependencyProperty TextBlockVisibilityProperty =
  15. DependencyProperty.Register("TextBlockVisibility", typeof(Visibility), typeof(EditableTextBlock),
  16. new PropertyMetadata(Visibility.Visible));
  17. // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
  18. public static readonly DependencyProperty TextProperty =
  19. DependencyProperty.Register("Text", typeof(string), typeof(EditableTextBlock),
  20. new PropertyMetadata(default(string)));
  21. // Using a DependencyProperty as the backing store for EnableEditing. This enables animation, styling, binding, etc...
  22. public static readonly DependencyProperty EnableEditingProperty =
  23. DependencyProperty.Register("IsEditing", typeof(bool), typeof(EditableTextBlock),
  24. new PropertyMetadata(OnIsEditingChanged));
  25. public EditableTextBlock()
  26. {
  27. InitializeComponent();
  28. }
  29. public Visibility TextBlockVisibility
  30. {
  31. get => (Visibility) GetValue(TextBlockVisibilityProperty);
  32. set => SetValue(TextBlockVisibilityProperty, value);
  33. }
  34. public bool IsEditing
  35. {
  36. get => (bool) GetValue(EnableEditingProperty);
  37. set => SetValue(EnableEditingProperty, value);
  38. }
  39. public string Text
  40. {
  41. get => (string) GetValue(TextProperty);
  42. set => SetValue(TextProperty, value);
  43. }
  44. private static void OnIsEditingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  45. {
  46. if ((bool) e.NewValue)
  47. {
  48. EditableTextBlock tb = (EditableTextBlock) d;
  49. tb.EnableEditing();
  50. }
  51. }
  52. public void EnableEditing()
  53. {
  54. ShortcutController.BlockShortcutExecution = true;
  55. TextBlockVisibility = Visibility.Hidden;
  56. IsEditing = true;
  57. textBox.Focus();
  58. textBox.SelectAll();
  59. }
  60. private void DisableEditing()
  61. {
  62. TextBlockVisibility = Visibility.Visible;
  63. ShortcutController.BlockShortcutExecution = false;
  64. IsEditing = false;
  65. }
  66. private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
  67. {
  68. if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2) EnableEditing();
  69. }
  70. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  71. {
  72. if (e.Key == Key.Enter) DisableEditing();
  73. }
  74. private void TextBox_LostFocus(object sender, RoutedEventArgs e)
  75. {
  76. DisableEditing();
  77. }
  78. private void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  79. {
  80. DisableEditing();
  81. }
  82. }
  83. }