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 ...
随机推荐
- 安卓开发_浅谈ListView之分页列表
前言: 在开发的过程中,有时候我们需要从网络解析一些数据,比如最近的一些新闻,我们需要把这些数据用ListView显示出来. 因为是解析一个网络数据源,这样将会一下子将所有的数据解析出来,当数据源数据 ...
- 《Inside C#》笔记(十三) 多线程 下
一 任务调度 当一个线程的时间片被用尽后,处理器会切换到另一个线程,但关于如何确定执行哪一个线程呢,这就涉及到了线程或任务的优先级. a) 每个线程都有优先级,任务调度算法会根据各线程的不同优先级来决 ...
- 实战:阿里巴巴 DevOps 转型后的运维平台建设
导读:阿里巴巴DevOps转型之后,运维平台是如何建设的?阿里巴巴高级技术专家陈喻结合运维自身的理解,业务场景的分析和业界方法论的一些思考,得出来一些最佳实践分享给大家. 前言 “我是这个应用 ...
- Python基础知识:字典
1.字典中键-值为一对,keys()返回一个列表,包含字典中所有键,values()返回所有值 favorite_languages ={ 'jack':"python", 'al ...
- VMWare:vSphere6 企业版参考序列号
HV4WC-01087-1ZJ48-031XP-9A843 NF0F3-402E3-MZR80-083QP-3CKM2 4F6FX-2W197-8ZKZ9-Y31ZM-1C3LZ JZ2E9-6D2D ...
- 6.1Python文件的操作(一)
目录 目录 前言 (一)基础类型 ==1.只读== ==2.只写== ==3.追加== (二)b二进制组合 ==1.读写二进制文件== (三)+ 附加组合 ==1.读附加== ==2.写附加== == ...
- 使用Jenkins+gitlab自动化构建时排除分支
我们的目的是gitlab上的代码有变动时会自动向Jenkins发送web钩子请求,触发指定的动作: 但默认情况下,所有分支(如测试环境和预生产)的代码有变动时都会触发,此时可以在Jenkins的项目设 ...
- Sudoku(第二次作业)
这里是github 工具清单: 编程语言:C++ 编程IDE:XCode 效能分析工具:XCode 源代码管理平台:Github PSP2.1 PSP2.1 Personal Software Pro ...
- Wampserver虚拟机配置记录
原文地址:http://blog.csdn.net/clj9017/article/details/12705725 第一步 在http.conf 文件里面找到 ,开启 Virtual hosts # ...
- python五十八课——正则表达式(替换)
替换:sub(regex,repl,string,count,[flags=0]): 替换数据,返回字符串(已经被替换完成后的内容)subn(regex,repl,string,count,[flag ...