作为初学者,您可能希望让用户看看他们是否正在键入其中一个可用选项。
1)在线搜索“autocomplete combobox”。
2)检查这些出:
http://weblogs.asp.net/okloeten/archive/2007/11/12/5088649.aspx
http://www.codeproject.com/KB/WPF/WPFCustomComboBox.aspx
3)亦尝试:
<ComboBox IsEditable="true" TextSearch.TextPath="Content">
<ComboBoxItem Content="Hello"/>
<ComboBoxItem Content="World"/>
</ComboBox>
上面代码段是一个primite方式是提供一种“视觉指示“你正在寻找。如果用户键入'h',则'hello'将出现在输入文本框中。但是,这本身并不具有阻止用户输入非法字符的机制。
4)这是一个更高级的版本:
<ComboBox Name="myComboBox" IsEditable="true" KeyUp="myComboBox_KeyUp">
<ComboBoxItem Content="Hello"/>
<ComboBoxItem Content="World"/>
<ComboBoxItem Content="WPF"/>
<ComboBoxItem Content="ComboBox"/>
</ComboBox>
代码隐藏:
private void myComboBox_KeyUp(object sender, KeyEventArgs e)
{
// Get the textbox part of the combobox
TextBox textBox = myComboBox.Template.FindName("PART_EditableTextBox", myComboBox) as TextBox;
// holds the list of combobox items as strings
List<String> items = new List<String>();
// indicates whether the new character added should be removed
bool shouldRemove = true;
for (int i = 0; i < myComboBox.Items.Count; i++)
{
items.Add(((ComboBoxItem)myComboBox.Items.GetItemAt(i)).Content.ToString());
}
for (int i = 0; i < items.Count; i++)
{
// legal character input
if(textBox.Text != "" && items.ElementAt(i).StartsWith(textBox.Text))
{
shouldRemove = false;
break;
}
}
// illegal character input
if (textBox.Text != "" && shouldRemove)
{
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
textBox.CaretIndex = textBox.Text.Length;
}
}
在这里,我们不会让用户继续输入中,一旦我们发现,没有组合框项目从文本框中的文本开始。我们删除添加的角色并等待另一个角色。
小费我的WPF老师的过程中重复数十次:在WPF,你不应该使用的SelectedItem明确。这是收集意见的目的。 – 2011-01-05 22:34:36