EditableTextBlock.xaml.cs 3.8 KB

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