Home >Backend Development >C++ >How to Create a Disappearing Watermark in a WPF TextBox Using Attached Properties?
This code creates a disappearing watermark in a WPF TextBox using attached properties. Let's refactor and improve the code for clarity and maintainability. The original code has some redundancy and could be simplified.
Watermark in TextBox
This improved example demonstrates creating a placeholder text (watermark) in a TextBox that disappears when the user starts typing. We'll utilize attached properties for a clean and reusable solution.
1. Improved Attached Property and Watermark Service:
<code class="language-csharp">public static class WatermarkService { public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached("Watermark", typeof(object), typeof(WatermarkService), new FrameworkPropertyMetadata(null, OnWatermarkChanged)); public static object GetWatermark(DependencyObject obj) => obj.GetValue(WatermarkProperty); public static void SetWatermark(DependencyObject obj, object value) => obj.SetValue(WatermarkProperty, value); private static void OnWatermarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is TextBox textBox)) return; textBox.Loaded += TextBox_Loaded; textBox.TextChanged += TextBox_TextChanged; textBox.GotFocus += TextBox_GotFocus; } private static void TextBox_Loaded(object sender, RoutedEventArgs e) => UpdateWatermarkVisibility((TextBox)sender); private static void TextBox_TextChanged(object sender, TextChangedEventArgs e) => UpdateWatermarkVisibility((TextBox)sender); private static void TextBox_GotFocus(object sender, RoutedEventArgs e) => UpdateWatermarkVisibility((TextBox)sender); private static void UpdateWatermarkVisibility(TextBox textBox) { RemoveWatermark(textBox); if (string.IsNullOrEmpty(textBox.Text)) { ShowWatermark(textBox); } } private static void RemoveWatermark(TextBox textBox) { var layer = AdornerLayer.GetAdornerLayer(textBox); if (layer != null) { var adorners = layer.GetAdorners(textBox); if (adorners != null) { foreach (var adorner in adorners.OfType<WatermarkAdorner>()) { layer.Remove(adorner); } } } } private static void ShowWatermark(TextBox textBox) { var layer = AdornerLayer.GetAdornerLayer(textBox); if (layer != null) { layer.Add(new WatermarkAdorner(textBox, GetWatermark(textBox))); } } }</code>
2. WatermarkAdorner Class (Minor Improvements):
<code class="language-csharp">internal class WatermarkAdorner : Adorner { private readonly ContentPresenter contentPresenter; public WatermarkAdorner(UIElement adornedElement, object watermark) : base(adornedElement) { IsHitTestVisible = false; contentPresenter = new ContentPresenter { Content = watermark, Opacity = 0.5 }; // Removed unnecessary margin setting; let the watermark style handle positioning. } protected override int VisualChildrenCount => 1; protected override Visual GetVisualChild(int index) => contentPresenter; protected override Size MeasureOverride(Size constraint) { contentPresenter.Measure(AdornedElement.RenderSize); return AdornedElement.RenderSize; } protected override Size ArrangeOverride(Size finalSize) { contentPresenter.Arrange(new Rect(finalSize)); return finalSize; } }</code>
3. XAML Usage:
<code class="language-xaml"><TextBox x:Name="SearchTextBox"> <WatermarkService.Watermark> <TextBlock>Type here to search text</TextBlock> </WatermarkService.Watermark> </TextBox></code>
Improvements:
Loaded
, TextChanged
, and GotFocus
events for the TextBox, making it more concise and easier to understand. The UpdateWatermarkVisibility
method handles the logic for showing and hiding the watermark based on the TextBox's text.TextBox
type, avoiding potential casting exceptions.OfType<WatermarkAdorner>()
simplifies the removal of adorners.TextBlock
within the watermark.This improved version is more efficient, readable, and maintainable while achieving the same functionality. Remember to add appropriate styling to the TextBlock
within the Watermark
to control its appearance and positioning within the TextBox
.
The above is the detailed content of How to Create a Disappearing Watermark in a WPF TextBox Using Attached Properties?. For more information, please follow other related articles on the PHP Chinese website!