I have a ComboBox I am using in a WPF form that appears to be showing odd behavior, where text is cleared immediately from the ComboBox after selecting a dropdown item, then typing text into the ComboBox.
First for some background information, my ComboBox contains a list of ComboBoxItems, with the Content attributes set to a variety of string values (in my sample application, I am using fruit names). The ComboBox itself has all properties set to default, except for the IsEditable property (set to true) and the IsTextSearchEnabled property (set to false).
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="121,100,0,0" IsEditable="True" KeyUp="comboBox_KeyUp" IsTextSearchEnabled="False">
<ComboBoxItem Content="apple"/>
<ComboBoxItem Content="banana"/>
<ComboBoxItem Content="grape"/>
<ComboBoxItem Content="lemon"/>
<ComboBoxItem Content="strawberry"/>
</ComboBox>
I am also using a basic filter that is triggered during the combobox's KeyUp event:
private void comboBox_KeyUp(object sender, KeyEventArgs e)
{
var combobox = (ComboBox)sender;
combobox.IsDropDownOpen = true;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(combobox.Items);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(combobox.Text))
{
return true;
}
else
{
if (((ComboBoxItem)o).Content.ToString().StartsWith(combobox.Text, true, null))
{
return true;
}
else
{
return false;
}
}
});
itemsViewOriginal.Refresh();
}
The issue occurs when I select an item from the dropdown list, click into the combobox to remove the highlight and put focus at the end of the word, then type. As soon as I enter a single letter, the text is removed immediately, and any new text I type essentially restarts the filter. I would expect the new text to simply append to the existing text in the combobox, but that is not the case here.
Interestingly, I can workaround the issue in 2 ways:
a) Re-enable "IsTextSearchEnabled." However, I do not want this property enabled for a variety of reasons, mainly being the behavior not being consistent with how the client expects the control to behave.
b) Modify the filter to use a static value.
If I modify the filter from being:
if (((ComboBoxItem)o).Content.ToString().StartsWith(combobox.Text, true, null))
to:
if (((ComboBoxItem)o).Content.ToString().StartsWith("ba", true, null))
then the clearing text behavior no longer occurs. However, this obviously does not help me since I want to use the ComboBox's text as the filter for the list.
What can I do to keep the text from being removed from the control in this scenario, while still maintaining the filtering and general control behavior?
Itemscollection is asking for trouble.ComboBoxattempts to synchronize the selected item with theTextproperty. When you filterItemssuch that it's effectively empty, the combo box apparently thinks, "Oh, there's no possible selection, so my current selection cannot possibly be valid. I'll go ahead and clear it out, along with my text."ComboBoxprobably isn't the best control to use for this scenario. I'd suggest you look for a third-party text control with auto completion that you can use instead. - Mike Strobel