在进行一些输入提示时,使用水印的效果是好于再使用一个Label添加内容的。看下如下效果:
觉得有用的小伙伴可以继续往下看。
我这个测试Demo是WPF + MVVM的。输入名称做了一个数据绑定。以下是代码,最后我把工程地址也加上。供大家免费下载。
Window.xaml.cs
<Window x:Class="TextBoxVerification.MainWindow"
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:TextBoxVerification"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="请输入用户名"/>
</VisualBrush.Visual>
</VisualBrush>
<Style x:Key="TextBoxBaseStyle" TargetType="TextBox">
<Setter Property="MinHeight" Value="26" />
<Setter Property="Background" Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush" Value="#D6DEE0" />
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CaretBrush" Value="{DynamicResource PrimaryTextBrush}" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Padding" Value="3" />
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border x:Name="border" CornerRadius="4" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer Padding="{TemplateBinding Padding}" Margin="-2,0,-1,0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" TargetName="border" Value="{StaticResource HintText}"/>
<Setter Property="BorderBrush" TargetName="border" Value="red"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" TargetName="border" Value="{StaticResource HintText}"/>
<Setter Property="BorderBrush" TargetName="border" Value="red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="gridTest" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<TextBox x:Name="text" Width="150" Height="30" Margin="5" Text="{Binding PersonID, Mode=TwoWay}" Style="{StaticResource TextBoxBaseStyle}">
</TextBox>
<Button Grid.Row="2" Width="150" Height="30" Content="test" Click="Button_Click"></Button>
<StackPanel Grid.Row="1" Width="150">
<Label Content="请输入用户名"></Label>
<TextBox Height="30"></TextBox>
</StackPanel>
</Grid>
</Window>
Window.cs
using System.Windows;
namespace TextBoxVerification
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ViewModel ViewModel;
public MainWindow()
{
InitializeComponent();
ViewModel = new ViewModel();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DataContext = ViewModel;
ViewModel.PersonID = "456";
}
}
}
NotifyObject.cs(数据绑定使用)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextBoxVerification
{
public class NotifyObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 属性发生改变时调用该方法发出通知
/// </summary>
/// <param name="propertyName">属性名称</param>
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected virtual void SetAndNotifyIfChanged<T>(string propertyName, ref T oldValue, T newValue)
{
if (oldValue == null && newValue == null) return;
if (oldValue != null && oldValue.Equals(newValue)) return;
if (newValue != null && newValue.Equals(oldValue)) return;
oldValue = newValue;
RaisePropertyChanged(propertyName);
}
}
}
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextBoxVerification
{
class ViewModel : NotifyObject
{
private string _personID = "123";
public string PersonID
{
get { return _personID; }
set
{
_personID = value;
RaisePropertyChanged("PersonID");
}
}
}
}
免费工程下载路径:https://download.csdn.net/download/chulijun3107/87647332