wpf 可筛选下拉框
作者:互联网
using System.Collections.Generic; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Input; namespace WpfApp25 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var nameList = new List<string> { "A0-Word","B0-Word","C0-Word", "A1-Word","A111","A11122","B1-Word","C1-Word", "B2-Word","C2-Word", "C3-Word", }; comboBox1.DataContext = nameList; comboBox1.SelectedValue = "aaa"; comboBox1.Loaded += delegate { System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as System.Windows.Controls.TextBox; Popup popup = comboBox1.Template.FindName("PART_Popup", comboBox1) as Popup; if (textBox != null) { textBox.KeyUp += (s, e) => { if (e.Key == Key.Down) { if (comboBox1.SelectedIndex==comboBox1.Items.Count-1) { return; } comboBox1.SelectedIndex += 1; return; } else if (e.Key==Key.Up) { if (comboBox1.SelectedIndex==-1) { return; } comboBox1.SelectedIndex -= 1; return; } comboBox1.Items.Filter += (item) => { string unSelected = textBox.Text; if (!string.IsNullOrEmpty(textBox.SelectedText) ) { unSelected = textBox.Text.Replace(textBox.SelectedText, ""); } if (item.ToString().ToLower().Contains(unSelected.ToLower())) { popup.IsOpen = true; return true; } else { return false; } }; }; } }; // comboBox1.KeyDown += new System.Windows.Input.KeyEventHandler(comboBox1_KeyDown); } private void comboBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == Key.Down) { e.Handled = true; } } } }
<Window x:Class="WpfApp25.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:WpfApp25" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <ComboBox Name="comboBox1" IsEditable="true" Width="200" Height="50" ItemsSource="{Binding}"> </ComboBox> </Grid> </Window>
标签:Word,Windows,System,下拉框,return,wpf,comboBox1,筛选,textBox 来源: https://www.cnblogs.com/chlm/p/12721514.html