To clarify I have a combobox with an observable collection bound to its itemsource property. I want user to be able to type in only items that ar present in the collection. whilst keeping the
IsTextSearchEnabled="true"
Functionality. So I've been looking online for an example on how to do this. Till now I figured i should use the
<ComboBox TextInput="cbb1_TextInput" />
Event and then searching the collection. But me feeble attempt's cant maintain the auto-complete feature, selectedindex gets jumbled and other unwanted behavior. Since I'm quite a noob in wpf I wonder if there are solutions that only use the XAML part?
EDIT: I tried something like this:
private void fase1cbb1_KeyUp(object sender, KeyEventArgs e)
{
ComboBox cb = (ComboBox)sender;
bool match = false;
TextBox tb = (TextBox)cb.Template.FindName("PART_EditableTextBox", cb);
if (tb.Text.Length > 0)
{
foreach (MenuItem MI in cb.Items)
{
if (MI.Text.StartsWith(tb.Text))
{
match = true; ;
}
}
if (!match)
{
int len = tb.Text.Length;
if (len > 0)
{
tb.Text = tb.Text.Substring(0, len - 1);
tb.SelectionStart = len;
}
}
}
}
But as soon as there is no match anymore there is no more selected item, and no more autocomplete/textsearch.
thnx for any advice or examples.
SOLUTION: WPF ComboBox with IsEditable="True" - How can I indicate that no match was found?
If you scroll all the way down to the bottom of the ComboBox documentation on MSDN you'll find that there's one very simple possible answer: set your ComboBox.IsEditable
to false. The user can still select items in the editor by typing the prefix into the box, but they'll only be able to enter values that already appear in the list.
The downside is that you don't get any of the normal "TextBox"-like behavior, in particular, you can't copy/paste the selected item out of the combo box. If that's also an issue for you, let us know, but that is the typical behavior for DropDownList style ComboBoxes in Windows anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With