原文: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的更多相关文章

  1. 【jQuery】输入框自带清除按钮

    最近一个项目,需要在输入框时右边出现“X”标志,点击X即可清空,主要使用了click和blur事件,难点在于点击‘X’时,input框获得焦点时出现“X”标志,而点击"x"标志时i ...

  2. ExtJS4 带清除功能的文本框 triggerfield

    Ext.onReady(function () { Ext.create('Ext.form.FormPanel', { title: 'Form with TriggerField', bodyPa ...

  3. android自定义文本框,后面带清空按钮

    android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...

  4. UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等

    (1)可以根据需要设置文本框的样式(包括形状.边框颜色.背景等). (2)可以根据需要设置文字显示样式(包括输入密码时的密文显示.文字横向居中.纵向居中上下.输入的文字是否首席木大写.文字超过后是否缩 ...

  5. 去除IE10自带的清除按钮

    最近在工作中碰到了一个问题,原本在IE8,IE9下正常的input表单,在IE10下会出现清除按钮,即表单右侧会出现一个可以清除该表单内容的小叉.由于之前一直没有兼容过IE10,所以我专门搜了下原因. ...

  6. JS,JQ及时监听input值的变化,MUI的input搜索框里的清除按钮的点击监听事件

    JS: document.getElementById("input对象的ID").addEventListener('input',function(){ console.log ...

  7. 带清空按钮TextBox的实现(WPF)

    本博文针对人群:WPF新手.博文内容:通过Style制定包含清空Button的TextBox样式模板,通过在Style中引入自定义类的附加属性完成对TextBox的内容清空. <span sty ...

  8. 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。

    模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...

  9. Dev中自带添加、编辑、删除等按钮的文字颜色等修改

    下面是ASPxGridView的自带按钮的文字等修改 <SettingsCommandButton> <NewButton Text=" " Image-Tool ...

随机推荐

  1. 【转】用yum只下载rpm包而不安装

    转自:http://liucheng.name/1950/ CentOS用yum安装软件是非常方便的,有时,我们只需要下载其中的rpm包,而不直接安装时咋办呢? 一般情况下,yum是不提供只下载的功能 ...

  2. vue.js的安装

    使用nodejs安装Vue-cli 1.安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(npm install --gl ...

  3. Info.plist的CFBundleIdentifier、CFBundleName、BundleDisplayName

    plist关键字段: CFBundleIdentifier:应用包名.唯一标识 CFBundleVersion:文件版本号,可以每次发版本递增 CFBundleShortVersionString:a ...

  4. sysbench对MySQL的压测,使用sysbench压测磁盘io

    QPS - query per secondTPS - transaction per second 不是特别关注,每个业务场景中事务标准是不一样的 Ⅰ.sysbench测试框架 Ⅱ.常用测试脚本 [ ...

  5. [HDFS_2] HDFS 的 Shell 操作

    0. 说明 在 Shell 下完成对 HDFS 的增删改查操作 1. 在 Shell 下完成对 HDFS 的增删改查操作 [1.0 查看帮助] [centos@s101 ~]$ hdfs dfs -h ...

  6. tkinter学习系列之(六)Radiobutton控件

    目录 目录 前言 (一)基本属性 (二)在Frame里布局: 目录 前言 Radiobutton单选框,在一组选框中,只能选中一个. (一)基本属性 (1)特有属性: value 按钮的值 varia ...

  7. Python实现Excel转换工具小结

    经历过的打表工具从c++.C#,再到Python,算下来还是Python方便些.一天即可上手开发,非常适合快速迭代中的各种小工具开发. Python开源的第三方库很多,涉及excel方面的也有好几个x ...

  8. 接上篇:将OneDrive云盘挂载到我的电脑!(1024快乐,明年我应该也可以过这个节日了!)

    今天对程序猿来说是个值得纪念的日子!祝程序员小哥哥小姐姐们今天可以早早下班,回家休息,Bug走开! 接上篇,将自己申请的5T云盘挂载到我的电脑! 第一步:挂网下载Raidrive 附上链接: http ...

  9. 寒假集训——搜索 D - Cubes for Masha

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...

  10. swift的@objc总结

    One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @objc相 ...