WatermarkTextBox.cs 813 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Windows;
  2. namespace HandyControl.Controls;
  3. public class WatermarkTextBox : TextBox
  4. {
  5. #region Public Properties
  6. #region Watermark
  7. public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register(
  8. nameof(Watermark), typeof(object), typeof(WatermarkTextBox), new PropertyMetadata(default(object)));
  9. public object Watermark
  10. {
  11. get => GetValue(WatermarkProperty);
  12. set => SetValue(WatermarkProperty, value);
  13. }
  14. #endregion Watermark
  15. #endregion Public Properties
  16. protected override void OnGotFocus(RoutedEventArgs e)
  17. {
  18. base.OnGotFocus(e);
  19. if (IsEnabled)
  20. {
  21. if (!string.IsNullOrEmpty(Text))
  22. {
  23. Select(0, Text.Length);
  24. }
  25. }
  26. }
  27. }