TextBoxPlaceholderBehaviour.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using Microsoft.Xaml.Behaviors;
  7. namespace InABox.WPF;
  8. public class TextBoxPlaceholderBehaviour : Behavior<TextBox>
  9. {
  10. public static readonly DependencyProperty TextProperty =
  11. DependencyProperty.Register(
  12. nameof(Text),
  13. typeof(String),
  14. typeof(TextBoxPlaceholderBehaviour),
  15. new PropertyMetadata(""));
  16. public String Text
  17. {
  18. get => (String)GetValue(TextProperty);
  19. set
  20. {
  21. SetValue(TextProperty, value);
  22. UpdateAssociatedObject(true);
  23. }
  24. }
  25. public static readonly DependencyProperty TextColorProperty =
  26. DependencyProperty.Register(
  27. nameof(TextColor),
  28. typeof(System.Windows.Media.Color),
  29. typeof(TextBoxPlaceholderBehaviour),
  30. new PropertyMetadata(Colors.Gray));
  31. public System.Windows.Media.Color TextColor
  32. {
  33. get => (System.Windows.Media.Color)GetValue(TextColorProperty);
  34. set
  35. {
  36. SetValue(TextColorProperty, value);
  37. UpdateAssociatedObject(true);
  38. }
  39. }
  40. private readonly PropertyDescriptor _propertyDescriptor =
  41. DependencyPropertyDescriptor.FromProperty(TextBox.BackgroundProperty, typeof(TextBox));
  42. private Brush? _originalBackground;
  43. private Brush? _placeholderBackground;
  44. protected override void OnAttached()
  45. {
  46. base.OnAttached();
  47. // Store the Background that has been set on the Text Box (usu through XAML)
  48. _originalBackground = AssociatedObject.Background;
  49. // Start Monitoring changes to the Associated object Background property
  50. _propertyDescriptor.AddValueChanged(AssociatedObject, OnBackgroundChanged);
  51. AssociatedObject.TextChanged += OnTextChanged;
  52. AssociatedObject.SizeChanged += OnSizeChanged;
  53. UpdateAssociatedObject();
  54. }
  55. protected override void OnDetaching()
  56. {
  57. AssociatedObject.SizeChanged -= OnSizeChanged;
  58. AssociatedObject.TextChanged -= OnTextChanged;
  59. _propertyDescriptor?.RemoveValueChanged(AssociatedObject,OnBackgroundChanged);
  60. AssociatedObject.Background = _originalBackground;
  61. base.OnDetaching();
  62. }
  63. private void OnBackgroundChanged(object? sender, EventArgs e)
  64. {
  65. // Update the Saved Background (usu a color, but might not be?)
  66. _originalBackground = AssociatedObject.Background;
  67. UpdateAssociatedObject(true);
  68. }
  69. private void OnTextChanged(object sender, TextChangedEventArgs e)
  70. {
  71. UpdateAssociatedObject();
  72. }
  73. private void SetBackground(Brush? brush)
  74. {
  75. _propertyDescriptor.RemoveValueChanged(AssociatedObject, OnBackgroundChanged);
  76. AssociatedObject.Background = brush;
  77. _propertyDescriptor.AddValueChanged(AssociatedObject, OnBackgroundChanged);
  78. }
  79. private void OnSizeChanged(object sender, SizeChangedEventArgs e)
  80. {
  81. UpdateAssociatedObject(true);
  82. }
  83. private Brush CreatePlaceholder()
  84. {
  85. return new VisualBrush()
  86. {
  87. Visual = new Label()
  88. {
  89. Content = Text,
  90. Margin = new Thickness(0),
  91. Padding = new Thickness(4, 0, 2, 0),
  92. HorizontalAlignment = HorizontalAlignment.Stretch,
  93. VerticalAlignment = VerticalAlignment.Stretch,
  94. HorizontalContentAlignment = AssociatedObject.HorizontalContentAlignment,
  95. VerticalContentAlignment = AssociatedObject.VerticalContentAlignment,
  96. FontFamily = AssociatedObject.FontFamily,
  97. FontStretch = AssociatedObject.FontStretch,
  98. FontSize = AssociatedObject.FontSize,
  99. FontWeight = AssociatedObject.FontWeight,
  100. FontStyle = FontStyles.Italic,
  101. Background = _originalBackground?.Clone(),
  102. Foreground = new SolidColorBrush(TextColor),
  103. Width = AssociatedObject.ActualWidth,
  104. Height = AssociatedObject.ActualHeight
  105. },
  106. Stretch = Stretch.None,
  107. TileMode = TileMode.None,
  108. AlignmentX = AlignmentX.Left,
  109. AlignmentY = AlignmentY.Center
  110. };
  111. }
  112. private void UpdateAssociatedObject(bool force = false)
  113. {
  114. if (force)
  115. _placeholderBackground = null;
  116. if (String.IsNullOrWhiteSpace(AssociatedObject.Text) && _placeholderBackground == null)
  117. {
  118. _placeholderBackground = CreatePlaceholder();
  119. SetBackground(_placeholderBackground);
  120. }
  121. else if (!String.IsNullOrWhiteSpace(AssociatedObject.Text) && _placeholderBackground != null)
  122. {
  123. _placeholderBackground = null;
  124. SetBackground(_originalBackground);
  125. }
  126. }
  127. }