WPF有水印的TextBox(WaterMark)

之前已经有实现过一个水印的TextBox,思路是TextBox在捕获和丢失焦点时,根据Text的值是否为空,修改Text的颜色和值,来达到水印目的。但是这种操作方法其实有一个明显的弊端,WPF用的最多的就是Binding,TextBox的Text属性用于Binding的就更多了;不仅如此,我们有时还希望自己能够定义水印的名称。因此,通过修改TextBox的控件模板,实现水印是一个更好的选择。

代码很简单,直接上:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyWPFControlsLib">


    <Style TargetType="{x:Type local:MyWatermark}">
        <Setter Property="BorderBrush" Value="Gray"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyWatermark}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"  SnapsToDevicePixels="True">
                        <Grid>
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <TextBlock x:Name="Message" VerticalAlignment="Center" Foreground="LightGray" Padding="3,0,0,0" Text="{TemplateBinding WaterMark}" Visibility="Hidden"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">
                            <Setter TargetName="Message" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

下面的这张图片,请注意,这里我用了个DataTrigger,为什么这么用,请自行理解:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace MyWPFControlsLib
{
    public class MyWatermark:TextBox
    {
        static MyWatermark()
        {
            //这句话就是告诉应用程序可以在Themes/Generic文件夹下找到样式,否则,另一个程序集使用则需要添加类似如何的<!--<ResourceDictionary Source="pack://application:,,,/MyWPFControlsLib;component/Themes/Generic.xaml"/>-->引用
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyWatermark), new FrameworkPropertyMetadata(typeof(MyWatermark)));
        }

        public static readonly DependencyProperty WaterMarkProperty = DependencyProperty.Register("WaterMark", typeof(string), typeof(MyWatermark), new PropertyMetadata("请输入文本"));

        public string WaterMark
        {
            get { return (string)GetValue(WaterMarkProperty); }
            set { SetValue(WaterMarkProperty, value); }
        }
    }
}

以上是控件的全部代码,你可以编译成dll,然后引用并使用它。最后上使用的时的代码,并附个效果图:

<Window x:Class="MyWPFControls.Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:MyWPFControlsLib;assembly=MyWPFControlsLib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <my:MyWatermark Width="200" Height="25" Padding="3" WaterMark="请输入数值"/>
    </Grid>
</Window>

到此全部结束。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值