此代碼使用附件屬性在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中文網其他相關文章!