此代码使用附件属性在WPF文本框中创建一个消失的水印。让我们重构并改进代码以清晰度和可维护性。 原始代码具有一定的冗余,可以简化。
>
文本框中的水印
这个改进的示例演示了在用户开始键入时消失的文本框中创建占位符文本(水印)。 我们将利用附加的属性来清洁可重复使用的解决方案。
>1。改进的附件和水印服务:
><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类(次要改进):
<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用法:
<code class="language-xaml"><TextBox x:Name="SearchTextBox"> <WatermarkService.Watermark> <TextBlock>Type here to search text</TextBlock> </WatermarkService.Watermark> </TextBox></code>
改进:>
Loaded
和TextChanged
> GotFocus
事件,使文本框更加简洁,更易于理解。 UpdateWatermarkVisibility
方法处理基于文本框的文本显示和隐藏水印的逻辑。TextBox
OfType<WatermarkAdorner>()
TextBlock
>。TextBlock
。
以上是如何使用附件的属性在WPF文本框中创建一个消失的水印?的详细内容。更多信息请关注PHP中文网其他相关文章!