WPF 带清除按钮的文字框SearchTextBox
基于TextBox的带清除按钮的搜索框
样式部分:
<!--带清除按钮文字框-->
<Style TargetType="{x:Type local:XSearchTextBox}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:XSearchTextBox}">
<Border CornerRadius="0" BorderBrush="#FFE2E0E0" BorderThickness="1" Padding="0,0,5,0" Background="{TemplateBinding Background}">
<DockPanel LastChildFill="True">
<Button Width="16" Height="16" DockPanel.Dock="Right" x:Name="PART_ContentHostClearButton">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="PART_Border" CornerRadius="8" BorderBrush="Transparent" BorderThickness="0" Padding="2">
<Path x:Name="PART_Path" Data="M6,6 L6,6 10,10 M10,6 L10,6 6,10" Fill="Gray" Stretch="Fill" Stroke="Gray" StrokeThickness="2" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="PART_Border">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Silver" Offset="0.0" />
<GradientStop Color="White" Offset="0.5" />
<GradientStop Color="Silver" Offset="0.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Stroke" TargetName="PART_Path" Value="#FFBA3232"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsFocused" Value="True" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button Width="16" Height="16" DockPanel.Dock="Right" Visibility="Collapsed" x:Name="PART_ContentHostSeachButton">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Path Name="searchPath" Stretch="Fill" Fill="Gray"
Data="F1 M 42.5,22C 49.4036,22 55,27.5964 55,34.5C 55,41.4036 49.4036,47 42.5,47C 40.1356,47 37.9245,46.3435 36,45.2426L 26.9749,54.2678C 25.8033,55.4393 23.9038,55.4393 22.7322,54.2678C 21.5607,53.0962 21.5607,51.1967 22.7322,50.0251L 31.7971,40.961C 30.6565,39.0755 30,36.8644 30,34.5C 30,27.5964 35.5964,22 42.5,22 Z M 42.5,26C 37.8056,26 34,29.8056 34,34.5C 34,39.1944 37.8056,43 42.5,43C 47.1944,43 51,39.1944 51,34.5C 51,29.8056 47.1944,26 42.5,26 Z ">
<Path.RenderTransform>
<RotateTransform Angle="-90" CenterX="8" CenterY="8" />
</Path.RenderTransform>
</Path>
</ControlTemplate>
</Button.Template>
</Button>
<ScrollViewer DockPanel.Dock="Left" Margin="2" x:Name="PART_ContentHost" Background="{TemplateBinding Background}"/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
代码部分:
public class XSearchTextBox : TextBox
{
static XSearchTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(XSearchTextBox), new FrameworkPropertyMetadata(typeof(XSearchTextBox)));
}
[CategoryAttribute("自定义属性"), DescriptionAttribute("获取或设置默认文字")]
public string PlaceHolder
{
get { return (string)GetValue(PlaceHolderProperty); }
set { SetValue(PlaceHolderProperty, value); }
}
[CategoryAttribute("自定义属性"), DescriptionAttribute("获取或设置默认文字")]
public static readonly DependencyProperty PlaceHolderProperty = DependencyProperty.Register("PlaceHolder", typeof(string), typeof(XSearchTextBox));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSeachButton", this) as ButtonBase;
searchBtn.Visibility = Visibility.Visible;
clearBtn.Visibility = Visibility.Collapsed;
this.Text = PlaceHolder;
this.Opacity = 0.4;
clearBtn.Click += (s, e) =>
{
this.Text = "";
};
this.LostFocus += (s, e) =>
{
if (this.Text.Length == 0)
{
this.Text = PlaceHolder;
this.Opacity = 0.4;
searchBtn.Visibility = Visibility.Visible;
clearBtn.Visibility = Visibility.Collapsed;
}
else
{
clearBtn.Visibility = Visibility.Visible;
searchBtn.Visibility = Visibility.Collapsed;
}
};
this.GotFocus += (s, e) =>
{
if (this.Text == PlaceHolder)
{
this.Text = "";
}
clearBtn.Visibility = Visibility.Visible;
searchBtn.Visibility = Visibility.Collapsed;
};
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
if (this.Text != PlaceHolder)
{
base.OnTextChanged(e);
}
if (this.Template != null)
{
ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSeachButton", this) as ButtonBase;
this.Opacity = 1;
if (this.Text.Length > 0)
{
if (this.Text != PlaceHolder)
{
clearBtn.Visibility = Visibility.Visible;
searchBtn.Visibility = Visibility.Collapsed;
}
else
{
this.Opacity = 0.4;
searchBtn.Visibility = Visibility.Visible;
clearBtn.Visibility = Visibility.Collapsed;
}
}
else
{
if (this.IsFocused)
{
clearBtn.Visibility = Visibility.Visible;
searchBtn.Visibility = Visibility.Collapsed;
}
else
{
this.Text = PlaceHolder;
}
}
}
}
}
WPF 带清除按钮的文字框SearchTextBox的更多相关文章
- 【jQuery】输入框自带清除按钮
最近一个项目,需要在输入框时右边出现“X”标志,点击X即可清空,主要使用了click和blur事件,难点在于点击‘X’时,input框获得焦点时出现“X”标志,而点击"x"标志时i ...
- ExtJS4 带清除功能的文本框 triggerfield
Ext.onReady(function () { Ext.create('Ext.form.FormPanel', { title: 'Form with TriggerField', bodyPa ...
- android自定义文本框,后面带清空按钮
android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...
- UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
(1)可以根据需要设置文本框的样式(包括形状.边框颜色.背景等). (2)可以根据需要设置文字显示样式(包括输入密码时的密文显示.文字横向居中.纵向居中上下.输入的文字是否首席木大写.文字超过后是否缩 ...
- 去除IE10自带的清除按钮
最近在工作中碰到了一个问题,原本在IE8,IE9下正常的input表单,在IE10下会出现清除按钮,即表单右侧会出现一个可以清除该表单内容的小叉.由于之前一直没有兼容过IE10,所以我专门搜了下原因. ...
- JS,JQ及时监听input值的变化,MUI的input搜索框里的清除按钮的点击监听事件
JS: document.getElementById("input对象的ID").addEventListener('input',function(){ console.log ...
- 带清空按钮TextBox的实现(WPF)
本博文针对人群:WPF新手.博文内容:通过Style制定包含清空Button的TextBox样式模板,通过在Style中引入自定义类的附加属性完成对TextBox的内容清空. <span sty ...
- 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。
模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...
- Dev中自带添加、编辑、删除等按钮的文字颜色等修改
下面是ASPxGridView的自带按钮的文字等修改 <SettingsCommandButton> <NewButton Text=" " Image-Tool ...
随机推荐
- 《Inside C#》笔记(六) 属性、数组、索引器
一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...
- 取消Eclipse等号、分号、空格代码自动补全
本文主要参考了以下文章 http://www.cnblogs.com/a-zx/p/3388041.html 本文基于 Eclipse Java EE IDE for Web Developers ...
- 机器学习实战(Machine Learning in Action)学习笔记————10.奇异值分解(SVD)原理、基于协同过滤的推荐引擎、数据降维
关键字:SVD.奇异值分解.降维.基于协同过滤的推荐引擎作者:米仓山下时间:2018-11-3机器学习实战(Machine Learning in Action,@author: Peter Harr ...
- sql server全文索引使用中的小坑 (转载)
一.业务场景 我们在实际生产环境中遇到了这样一种需求,即需要检索一个父子关系的子树数据 估计大家也遇到过类似的场景,最典型的就是省市数据,其中path字段是按层级关系生成的行政区路径: 如果我们已知某 ...
- Pyhon环境变量的一些坑
在正常的情况下,使用编译器执行Python文件,无需考虑环境变量的改变 例:sum --one --one1.py --two --two1.py 在执行one.py文件需要调用 two.py中某个方 ...
- centos7搭建SVN+Apache+IF.svnadmin支持https实现web管理SVN
阅读目录 1. 介绍 2. 软件准备 3. 建立SVN Server仓库 4. 配置安装PHP&IF.SVNadmin 5. 启动服务 1.介绍 公司最近想把Windows server平台的 ...
- July 10th, Week 29th Sunday, 2016
Everything is good when new, but friend when old. 老朋友更醇香. When did you meet with your last friends l ...
- [项目实践] python文件路径引用的规则,记一次使用sys.path[0]的问题,及如何区分 ../与 ./的使用场景
下面是一个获取配置的代码 def getValue(self,section,option): """ @file: string,the name of the con ...
- mvc、mvp和mvvm理解
MVC.MVP.MVVM这些模式是为了解决开发过程中的实际问题而提出来的,目前作为主流的几种架构模式而被广泛使用. 一.MVC(Model-View-Controller) MVC是比较直观的架构模式 ...
- 【转】windows 控制台cmd乱码的解决办法
windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...