Wpf 文本框模糊匹配

源码下载地址

实现效果如图
整体实现不怎么难就不一一介绍了,主要是用了Popup 以及keyup 事件
这里有一个问题是style 中绑定属性不能直接用(这个地方我准备有时间了改,不影响使用了)…

<UserControl x:Class="UI.IntellTextBox"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:UI"mc:Ignorable="d" x:Name="_base"xmlns:sys="clr-namespace:System;assembly=mscorlib"d:DesignHeight="450" d:DesignWidth="800"><Grid><Label Foreground="Silver" Content="{Binding ElementName=_base,Path=WaterTxt}"  ></Label><TextBox     KeyUp="TextBox_KeyUp" x:Name="tTxt" Text="{Binding  ElementName=_base,Path=ContentText,UpdateSourceTrigger=PropertyChanged}"  VerticalContentAlignment="Center"><TextBox.Style><Style TargetType="TextBox"><Style.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsFocused" Value="False"></Condition><Condition Property="Text" Value=""></Condition></MultiTrigger.Conditions><Setter Property="Background"><Setter.Value><VisualBrush AlignmentX="Left" AlignmentY="Top" Stretch="None"><VisualBrush.Visual><TextBlock   Background="Transparent" Foreground="Silver"  FontSize="14"  Text="{Binding ElementName=_base,Path=WaterTxt,UpdateSourceTrigger=PropertyChanged}"></TextBlock></VisualBrush.Visual></VisualBrush></Setter.Value></Setter></MultiTrigger></Style.Triggers></Style></TextBox.Style></TextBox><Popup x:Name="ConfigPopup" Height="auto" Width="150" StaysOpen="False"  AllowsTransparency="True"PopupAnimation="Slide" PlacementTarget="{Binding ElementName=Txttb}"  Placement="Bottom" IsOpen="False" HorizontalAlignment="Left"><Grid Width="auto" Height="auto"><ListBox x:Name="MailConfigSelection" KeyDown="MailConfigSelection_OnKeyDown" MouseLeftButtonUp="MailConfigSelection_MouseLeftButtonUp" ItemsSource="{Binding IntellList}" IsTextSearchEnabled="True"  ></ListBox></Grid></Popup></Grid>
</UserControl>

后端代码 这里用了通过方向键来控制listbox 中seleindex 的值
快速创建依赖属性 propdp +2Tab

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace UI
{/// <summary>/// IntellTextBox.xaml 的交互逻辑/// </summary>public partial class IntellTextBox : UserControl{static List<string> Treelist = new List<string>() { };public IntellTextBox(){InitializeComponent();}public IEnumerable<string> IntellList{get { return (IEnumerable<string>)GetValue(IntellListProperty); }set { SetValue(IntellListProperty, value); }}// Using a DependencyProperty as the backing store for IntellList.  This enables animation, styling, binding, etc...public static readonly DependencyProperty IntellListProperty =DependencyProperty.Register("IntellList", typeof(IEnumerable<string>), typeof(IntellTextBox), new PropertyMetadata(Treelist, ListChange));public string WaterTxt{get { return (string)GetValue(WaterTxtProperty); }set { SetValue(WaterTxtProperty, value); }}// Using a DependencyProperty as the backing store for WaterTxt.  This enables animation, styling, binding, etc...public static readonly DependencyProperty WaterTxtProperty =DependencyProperty.Register("WaterTxt", typeof(string), typeof(IntellTextBox), new PropertyMetadata(string.Empty, WaterTextChange));private static void WaterTextChange(DependencyObject d, DependencyPropertyChangedEventArgs e){IntellTextBox tobj = d as IntellTextBox;tobj.WaterTxt = (string)e.NewValue;//tobj.OnSamplePropertyChanged(e);}private void OnSamplePropertyChanged(DependencyPropertyChangedEventArgs e){string SamplePropertyNewValue = (string)e.NewValue;tTxt.Text = SamplePropertyNewValue;}public string ContentText{get { return (string)GetValue(ContentTextProperty); }set { SetValue(ContentTextProperty, value); }}// Using a DependencyProperty as the backing store for ContentText.  This enables animation, styling, binding, etc...public static readonly DependencyProperty ContentTextProperty =DependencyProperty.Register("ContentText", typeof(string), typeof(IntellTextBox), new PropertyMetadata(string.Empty, ContetChange));private static void ContetChange(DependencyObject d, DependencyPropertyChangedEventArgs e){IntellTextBox tobj = d as IntellTextBox;tobj.ContentText = (string)e.NewValue;// tobj.LoadText((string)e.NewValue);}private static void ListChange(DependencyObject d, DependencyPropertyChangedEventArgs e){IntellTextBox tobj = d as IntellTextBox;tobj.IntellList = (IEnumerable<string>)e.NewValue;}private void TextBox_KeyUp(object sender, KeyEventArgs e){TextBox tbm = e.OriginalSource as TextBox;if (IntellList.ToList().Count != 0)//这里是这样的条件,可以根据需求来改变{if (e.Key == Key.Enter){if (MailConfigSelection.SelectedItem == null) return;string mailConfig = MailConfigSelection.SelectedItem.ToString();TextBox tb = tTxt;int i = tb.CaretIndex;//获取呼出这个popup的textbox的当前光标位置tb.Text = mailConfig;//插入选择的字符串tb.CaretIndex = i + mailConfig.Length + 1;//移动光标tb.Focus();ConfigPopup.IsOpen = false;}if (e.Key == Key.Down){int index = MailConfigSelection.SelectedIndex;if (index < MailConfigSelection.Items.Count){index++;}MailConfigSelection.SelectedIndex = index;}if (e.Key == Key.Up){int index = MailConfigSelection.SelectedIndex;if (index > 1){index--;}MailConfigSelection.SelectedIndex = index;}else if (e.Key != Key.Down && e.Key != Key.Up && e.Key != Key.Enter)ShowPopUp(tbm.GetRectFromCharacterIndex(tbm.CaretIndex), tbm);}}private void ShowPopUp(Rect placementRect, TextBox tb){// ConfigPopup.PlacementTarget = tb;//ConfigPopup.PlacementRectangle = placementRect;ConfigPopup.IsOpen = true;List<string> resultList = (from c in IntellList where c.Contains(tb.Text) select c).ToList();MailConfigSelection.ItemsSource = resultList;MailConfigSelection.SelectedIndex = 0;var listBoxItem = (ListBoxItem)MailConfigSelection.ItemContainerGenerator.ContainerFromItem(MailConfigSelection.SelectedItem);if (listBoxItem != null) listBoxItem.Focus();}private void MailConfigSelection_OnKeyDown(object sender, KeyEventArgs e){if (e.Key == Key.Enter){ConfigPopup.IsOpen = false;System.Windows.Controls.ListBox lb = sender as System.Windows.Controls.ListBox;if (lb == null) return;string mailConfig = lb.SelectedItem.ToString();//Popup pp = (lb.Parent as Grid).Parent as Popup;TextBox tb = ConfigPopup.PlacementTarget as TextBox;int i = tb.CaretIndex;//获取呼出这个popup的textbox的当前光标位置tb.Text = mailConfig;//插入选择的字符串tb.CaretIndex = i + mailConfig.Length + 1;//移动光标tb.Focus();ConfigPopup.IsOpen = false;}else if (e.Key == Key.Escape){}}private void MailConfigSelection_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){ConfigPopup.IsOpen = false;System.Windows.Controls.ListBox lb = sender as System.Windows.Controls.ListBox;if (lb == null) return;if (lb.SelectedItem != null){string mailConfig = lb.SelectedItem.ToString();TextBox tb = tTxt;int i = tb.CaretIndex;//获取呼出这个popup的textbox的当前光标位置tb.Text = mailConfig;//插入选择的字符串tb.CaretIndex = i + mailConfig.Length + 1;//移动光标tb.Focus();ConfigPopup.IsOpen = false;}}}
}

使用

<Window x:Class="Indistinct.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Indistinct"xmlns:ui="clr-namespace:UI;assembly=UI"mc:Ignorable="d"x:Name="_base"Title="模糊匹配" Height="450" Width="800"><Grid><ui:IndisTinctTextBox Width="100" Height="30" WaterTxt="这是水印"  x:Name="IndTxt"  IntellList="{Binding SaleList}"></ui:IndisTinctTextBox></Grid>
</Window>

Wpf 文本框模糊匹配相关推荐

  1. input 模糊匹配功能 文本框模糊匹配(纯html+jquery简单实现) demo

    input 模糊匹配功能 文本框模糊匹配(纯html+jquery简单实现) demo <!DOCTYPE HTML> <html lang="en"> & ...

  2. WPF 文本框添加水印效果

    有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...

  3. 电子邮件地址验证:详细解释,生产质量WPF文本框代码

    目录 介绍 电邮地址格式 1. Address 2. addr-spec 有效的电子邮件地址 显示名称 注释 引号 一些看起来很奇怪的有效地址 Domain-Part要求 不使用ASCII字符(UTF ...

  4. ajax下拉列表模糊,JS仿百度自动下拉框模糊匹配提示

    实际项目中,我们可以把数据获取改成ajax动态获取,在 getContent()中 js/jQuery实现类似百度搜索功能 #container { position: absolute; left: ...

  5. 如何在与 WPF 文本框的触摸交互中显示触摸键盘

    Windows Presentation Foundation (WPF) 应用程序在支持触控的世界中会遇到一些困难.在 WinRT 应用程序中,当文本字段获得焦点时,触摸键盘会自动显示,这样用户就可 ...

  6. wpf文本框限制输入长度_Excel办公实操,限制输入日期,手机号码,不重复数据的使用...

    限制只能输入当前日期之前的日期 图是某企业的入库登记表,用来登记不同日期各品名和型号的入库信息.其中A列是产品入库的日期,要求必须为当前日期之前的日期,为了防止工作人员将入库日期输入为当天以后的日期, ...

  7. xpath爬取airbnb民宿价格信息,为啥用属性定位不到元素,但是用文本内容模糊匹配却可以

    超级小白,刚刚接触python.今天想要试着爬一下爱彼迎上的民宿信息,包括房屋情况.位置.价格三个信息: 我一开始的代码是这样的: 前两个信息都可以用find_elements_by_xpath很容易 ...

  8. Ext js 下拉框模糊匹配查询,并支持反复输入检索

    记录一下Ext js开发中遇到的下拉框检索需求. 由于数据多,Extjs的组件下拉框显示数据多,不好选择,需要能在输入框输入字段,然后下拉框只显示输入字段相关的内容,进行一个模糊查询,并且能够支持反复 ...

  9. C# WPF文本框TextEdit不以科学计数法显示

    01 - 前言 一个float或者double类型的数值,如果小数点后0的个数≥4,在界面上就会自动以科学计数法显示, 比如:0.00003会显示成这样 但是很多时候我并不希望它这样显示,因为这样不方 ...

最新文章

  1. 使用 Jenkins 部署码云上的 Spring Boot 项目
  2. 书籍推荐——按内容划分
  3. so文件反编译_安卓攻防so模块自动化修复实战
  4. JavaFX 一 出生新手村(阅读小规则)
  5. 答应了好久的camera资料
  6. LeetCode 81 搜索旋转排序数组 II
  7. linux ftp脚本
  8. Git笔记(25) 选择修订版本
  9. mysql 复制功能_MySQL实现主从复制功能
  10. 「猪齿鱼」助力汉得信息智能制造集中交付高效协同
  11. YApi 高级mock脚本 1.8.3版本后,mockJson不能正确返回问题
  12. 从SARS、埃博拉到新冠状病毒,技术在革命中进步
  13. 如何设置计算机自动连接宽带,宽带自动连接设置,小编教你电脑怎么设置宽带自动连接...
  14. 职业自我认知的测试软件,职业生涯规划自我认知测试.docx
  15. Android Studio改变安卓工具栏背景色,内容色和状态栏颜色
  16. c语言符号txt下载,C语言符号集
  17. C语言——基础指针篇
  18. Failed to load build-tools\xx\lib\dx.jar的解决方式
  19. cf手游服务器维护什么时候结束,cfhd正在维护什么意思
  20. 三相电压型逆变电路 解答题

热门文章

  1. 编写Bash脚本实现使用FFmpeg批量合并视频
  2. 『矩阵论笔记』张量CP分解的详细数学推导以及Python实现
  3. python编写猜数游戏代码、如果不是整数、显示输入错误_数字炸弹游戏程序 用python来实现...
  4. 习题HTML(web程序设计第8版)
  5. 名画182 仇英《清明上河图》
  6. PMP 常见100个考点和答题策略
  7. 请问你见过吐代码的泡泡吗(冒泡排序)
  8. 全网最详细的Mac下搭建Appium环境文档,没有之一
  9. navicat12安装注册集火
  10. 2019年全国马拉松报名时间指南