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 ...
随机推荐
- 安卓开发_浅谈WebView(转)
,有一个功能需要在APP中调用网站 百度了一下,发现需要用WebView来实现 实现方法很容易,我就不在这里写一遍了 ,直接转一下我学习的内容吧 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...
- 《Inside C#》笔记(五) 方法
方法用来体现类的行为. 一 相关概念 a) ref和out 通常一个方法只能返回一个值,但如果确实需要返回多个值时,可以使用ref或out.如果方法的参数被标注为ref或out,在调用该方法时,会传递 ...
- flow-vue.js移动端效果
得益于vue.js和element,以及vue-element-extends在线表格编辑.前后端分离的后端用golang+beego框架,服务器采用腾讯云. vue的自适应做的很好,只要将侧栏加一行 ...
- 动态切换 web 报表中的统计图类型
统计图在浏览器端展现时,不同的使用人员对图形的展现形式会有不同的要求,有的需要柱形图.有的想看折线图等,报表支持用户在浏览器端动态的选择统计图类型,关注乾学院,查看具体实现方法动态切换 web 报表中 ...
- (后台)org.apache.catalina.connector.ClientAbortException: null
比如错误日志是这样的: org.apache.catalina.connector.ClientAbortException: null 那么问题基本上就是服务器准备进行response的时候,发现连 ...
- JMeter—监听器(十二)
参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第七节监听器用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默 ...
- 将 Azure 文件共享用于 Windows VM
可将 Azure 文件共享用作一种从 VM 中存储和访问文件的方式. 例如,你可以存储一个要与所有 VM 共享的脚本或应用程序配置文件. 本主题将介绍如何创建和装载 Azure 文件共享,以及如何上传 ...
- 关于使用WeUI在IE中提示“font-face 未能完成 OpenType 嵌入权限检查。权限必须是可安装的。”的问题
@font-face是css3中定义字体的规则. 首先,在使用weui时,在Chrome.Firefox下没有问题,但是在IE下提示“font-face 未能完成 OpenType 嵌入权限检查.权限 ...
- DMZ 区域
下面对DMZ区域进行简要介绍:DMZ是网络的一个区域,介于外网与内网之间的一个特殊区域,也称隔离区.它不同于传统的防火墙设置,DMZ防火墙方案为要保护的内部网络增加了一道安全防线,通常认为是非常安全的 ...
- Django电商项目---完成用户中心(订单中心+收货地址)day7
完成用户中心(收货地址) df_user/views.py df_user/urls.py templates/df_user/user_center_site.html 界面显示 完成用户中心(全部 ...