EditableTextBlock.xaml.cs 3.7 KB

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