在WPF中自动完成Combobox [英] Autocomplete Combobox in WPF
本文介绍了在WPF中自动完成Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XML:
< ComboBox 高度 = 23 HorizontalAlignment = 左 保证金 = 160,236,0,0 名称 = comboBox1 < span class =code-attribute> VerticalAlignment = Top 宽度 = 120 IsEditable = True ItemsSource = {Binding} KeyDown = comboBox1_KeyDown >
< ComboBox。资源 >
< sys:Double x:Key = {x:Static SystemParameters.VerticalScrollBarWidthKey} > 0 < / sys:Double >
< span class =code-keyword>< / ComboBox.Resources >
< / ComboBox >
C#:
公共部分类MainWindow:Window
{
List< string>名单;
public MainWindow()
{
InitializeComponent();
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.Loaded + =委托
{
System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName(PART_EditableTextBox,comboBox1)as System.Windows.Controls 。文本框;
Popup popup = comboBox1.Template.FindName(PART_Popup,comboBox1)为Popup;
if(textBox!= null)
{
textBox.TextChanged + = delegate
{
comboBox1.Items.Filter + =(item)=>
{
if(item.ToString()。StartsWith(textBox.Text))
{
popup.IsOpen = true;
返回true;
}
else
{
// popup.IsOpen = false;
返回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;
comboBox1.Items.MoveCurrentToNext();
}
}
解决方案
仅供参考,WPF Toolkit()中有一个很好的自动完成文本框。为什么不使用它?
Guy's after trying to autocomplete a textbox i move towards now to an autocomplete combobox.Because i cannot add all controls to a textbox.But in combobox,i did that.But a problem also stands there.When i type in combobox it's popup filters the items according to typed text nicely,but i need to select a option by mouse,cannot by keyboard arrow's.I tried to solve it with keydown event but somehow it cannot focus combobox i think.Someone can suggest me here what wrong i am doing or how can i enable keyboard arrowkey events?Below are my codes:
XML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="160,236,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" IsEditable="True" ItemsSource="{Binding}" KeyDown="comboBox1_KeyDown">
<ComboBox.Resources>
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
</ComboBox.Resources>
</ComboBox>
C#:
public partial class MainWindow : Window { List<string> nameList; public MainWindow() { InitializeComponent(); 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.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.TextChanged += delegate { comboBox1.Items.Filter += (item) => { if (item.ToString().StartsWith(textBox.Text)) { popup.IsOpen = true; return true; } else { // popup.IsOpen = false; 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; comboBox1.Items.MoveCurrentToNext(); } }
解决方案
FYI, there is nice auto-complete text box in WPF Toolkit (). Why don`t you use it?
这篇关于在WPF中自动完成Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文