Home > Backend Development > C++ > How to Create a Disappearing Watermark in a WPF TextBox Using Attached Properties?

How to Create a Disappearing Watermark in a WPF TextBox Using Attached Properties?

Patricia Arquette
Release: 2025-01-29 23:01:10
Original
593 people have browsed it

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.

How to Create a Disappearing Watermark in a WPF TextBox Using Attached Properties?

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>
Copy after login

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>
Copy after login

3. XAML Usage:

<code class="language-xaml"><TextBox x:Name="SearchTextBox">
    <WatermarkService.Watermark>
        <TextBlock>Type here to search text</TextBlock>
    </WatermarkService.Watermark>
</TextBox></code>
Copy after login

Improvements:

  • Simplified Event Handling: The revised code uses only 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.
  • Removed Redundancy: The original code had several duplicated code blocks. This version streamlines the logic.
  • Improved Type Safety: The code now explicitly checks for TextBox type, avoiding potential casting exceptions.
  • LINQ Usage: Using OfType<WatermarkAdorner>() simplifies the removal of adorners.
  • Clearer Structure: The code is better organized into logical sections.
  • Removed Unnecessary Margin: The margin is better handled through styling the 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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template