我在 WPF 文本框中创建占位符。我在stackoverflow中得到了提示代码。
带有静态提示的工作代码
<Window x:Class="App.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:App"
mc:Ignorable="d"
Title="Test" Height="300" Width="300">
<Window.Resources>
<Style x:Key="TextBoxWithHintStyle" TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="search" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox Style="{StaticResource TextBoxWithHintStyle}" Height="auto" Padding="5px" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="auto" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"/>
</Grid>
</Window>
我正在对其进行自定义,以便它适用于所有文本框。
XAML 文件:
<Window x:Class="App.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:App"
mc:Ignorable="d"
Title="Test" Height="300" Width="300">
<Window.Resources>
<Style x:Key="TextBoxWithHintStyle" TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding Path=(local:TextBoxWithHint.Watermark),RelativeSource={RelativeSource Self}}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox Style="{StaticResource TextBoxWithHintStyle}" local:TextBoxWithHint.Watermark="Search" Height="auto" Padding="5px" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="auto" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"/>
</Grid>
</Window>
具有 DependecyProperty 类的文本框
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace App
{
public class TextBoxWithHint : TextBox
{
public static string GetWatermark(DependencyObject obj)
{
return (string)obj.GetValue(WatermarkProperty);
}
public static void SetWatermark(DependencyObject obj, string value)
{
obj.SetValue(WatermarkProperty, value);
}
public static readonly DependencyProperty WatermarkProperty =
DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxWithHint), new UIPropertyMetadata(string.Empty));
}
}
所以我想要写在 xaml 文件中的动态提示。谁能告诉我这有什么问题。我想在项目中使用不同的提示多次使用文本框。