首頁 > 後端開發 > C++ > 如何在WPF文本框中實施消失的水印?

如何在WPF文本框中實施消失的水印?

Mary-Kate Olsen
發布: 2025-01-29 22:56:12
原創
962 人瀏覽過

How to Implement Disappearing Watermarks in WPF TextBoxes?

文本框中的水印

>可以在用戶開始鍵入時自動消失的文本框中包含文本。 🎜>附加的屬性解決方案

>>此解決方案利用附件的屬性將水印功能添加到任何文本框中:

using System;
using System.Windows;
using System.Windows.Controls;

public static class WatermarkService
{
    public static DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached(
        "Watermark",
        typeof(object),
        typeof(WatermarkService),
        new FrameworkPropertyMetadata((object)null, OnWatermarkChanged)
    );

    public static object GetWatermark(DependencyObject d)
    {
        return (object)d.GetValue(WatermarkProperty);
    }

    public static void SetWatermark(DependencyObject d, object value)
    {
        d.SetValue(WatermarkProperty, value);
    }

    private static void OnWatermarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Control control = (Control)d;
        control.Loaded += Control_Loaded;

        if (d is TextBox)
        {
            control.GotKeyboardFocus += Control_GotKeyboardFocus;
            control.LostKeyboardFocus += Control_Loaded;
            ((TextBox)control).TextChanged += Control_GotKeyboardFocus;
        }
    }

    private static void Control_GotKeyboardFocus(object sender, RoutedEventArgs e)
    {
        Control c = (Control)sender;
        if (ShouldShowWatermark(c))
        {
            ShowWatermark(c);
        }
        else
        {
            RemoveWatermark(c);
        }
    }

    private static void Control_Loaded(object sender, RoutedEventArgs e)
    {
        Control control = (Control)sender;
        if (ShouldShowWatermark(control))
        {
            ShowWatermark(control);
        }
    }

    private static void RemoveWatermark(UIElement control)
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(control);

        if (layer != null)
        {
            Adorner[] adorners = layer.GetAdorners(control);
            if (adorners == null)
            {
                return;
            }

            foreach (Adorner adorner in adorners)
            {
                if (adorner is WatermarkAdorner)
                {
                    adorner.Visibility = Visibility.Hidden;
                    layer.Remove(adorner);
                }
            }
        }
    }

    private static void ShowWatermark(Control control)
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(control);

        if (layer != null)
        {
            layer.Add(new WatermarkAdorner(control, GetWatermark(control)));
        }
    }

    private static bool ShouldShowWatermark(Control c)
    {
        if (c is TextBox)
        {
            return (c as TextBox).Text == string.Empty;
        }
        else
        {
            return false;
        }
    }
}
登入後複製

> watermarkservice.cs

using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; internal class WatermarkAdorner : Adorner { private ContentPresenter contentPresenter; public WatermarkAdorner(UIElement adornedElement, object watermark) : base(adornedElement) { this.IsHitTestVisible = false; this.contentPresenter = new ContentPresenter(); this.contentPresenter.Content = watermark; this.contentPresenter.Opacity = 0.5; this.contentPresenter.Margin = new Thickness(Control.Margin.Left + Control.Padding.Left, Control.Margin.Top + Control.Padding.Top, 0, 0); // Hide the control adorner when the adorned element is hidden Binding binding = new Binding("IsVisible"); binding.Source = adornedElement; binding.Converter = new BooleanToVisibilityConverter(); this.SetBinding(VisibilityProperty, binding); } protected override int VisualChildrenCount { get { return 1; } } private Control Control { get { return (Control)this.AdornedElement; } } protected override Visual GetVisualChild(int index) { return this.contentPresenter; } protected override Size MeasureOverride(Size constraint) { this.contentPresenter.Measure(Control.RenderSize); return Control.RenderSize; } protected override Size ArrangeOverride(Size finalSize) { this.contentPresenter.Arrange(new Rect(finalSize)); return finalSize; } }

<AdornerDecorator>
    <TextBox x:Name="SearchTextBox">
        <controls:WatermarkService.Watermark>
            <TextBlock>Type here to search text</TextBlock>
        </controls:WatermarkService.Watermark>
    </TextBox>
</AdornerDecorator>
登入後複製

以上是如何在WPF文本框中實施消失的水印?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板