WPF TextBox添加水印提示

        在进行一些输入提示时,使用水印的效果是好于再使用一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚楚3107

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值