WPF自定义WaterTextBox
来源:互联网 发布:张一山黄金趋势源码 编辑:程序博客网 时间:2024/05/16 05:56
今天在看javaScript中发现有onblur和onfocus事件,很轻松了实现了有水印的TextBox,寻思着wpf中是不是也有类似的方法,查找官网api,发现了有LostFocus和GotFocus两个方法,不过,要实现类似效果,你的重写他。代码很简单,直接上:
public class WaterTextBox : TextBox
{
public WaterTextBox()
{
this.Text = "输入内容";
this.Foreground = Brushes.Gray;
}
protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
{
base.OnLostFocus(e);
if (string.IsNullOrEmpty(this.Text))
{
this.Text = "输入内容";
this.Foreground = Brushes.Gray;
}
}
protected override void OnGotFocus(System.Windows.RoutedEventArgs e)
{
base.OnGotFocus(e);
if (this.Text == "输入内容" && this.Foreground == Brushes.Gray)
{
this.Clear();
this.Foreground = Brushes.Black;
}
}
}
定义一个WaterTextBox继承TextBox,重写OnLostFocus和OnGotFocus,前台代码更简单,如下:
<Window x:Class="wpfLostFocus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:wpfLostFocus"
Title="MainWindow" Height="350" Width="525">
<Grid>
<my:WaterTextBox Width="100" Height="25"></my:WaterTextBox>
<TextBox Margin="208,198,209,92" Text="nihao"/>
</Grid>
</Window>
- WPF自定义WaterTextBox
- 自定义WPF面板
- WPF继承自定义窗口
- WPF ScrollViewer 自定义样式
- WPF自定义控件
- 自定义WPF面板
- WPF自定义控件选择
- 自定义WPF 窗口样式
- [WPF] 自定义窗体样式
- WPF自定义控件
- WPF之自定义窗体
- WPF创建自定义窗体
- [WPF] 自定义按钮样式
- WPF之自定义窗体
- wpf 自定义Tooltip template
- wpf 自定义scrollviewer
- WPF DataGrid 分页自定义
- WPF绘制自定义窗口
- mysql数据库从库同步延迟的问题
- 硬盘安装 红帽系列Linux
- 友盟统计时出的错误
- chkconfig_rh9.sh
- Android使用序列化接口Parcelable、Serializable
- WPF自定义WaterTextBox
- c#版本23个设计模式
- c#代码中捕获源信息
- dhcpd.conf.txt
- 普通用户从非80端口启动tomcat,通过端口转发监听80端口
- dns.txt
- 【新人笔记16.3.30】JS--数组
- 数据结构(四)--B树、B-树、B+树、B*树
- grub.conf