In a combobox, how do I determine the highlighted item (not selected item)?

Posted by Harold Bamford on Stack Overflow See other posts from Stack Overflow or by Harold Bamford
Published on 2010-04-27T18:39:31Z Indexed on 2010/04/27 18:53 UTC
Read the original article Hit count: 204

Filed under:
|

First, fair warning: I am a complete newbie with C# and WPF.

I have a combobox (editable, searchable) and I would like to be able to intercept the Delete key and remove the currently highlighted item from the list. The behavior I'm looking for is like that of MS Outlook when entering in email addresses. When you give a few characters, a dropdown list of potential matches is displayed. If you move to one of these (with the arrow keys) and hit Delete, that entry is permanently removed. I want to do that with an entry in the combobox.

Here is the XAML (simplified):


<ComboBox x:Name="Directory"
    KeyUp="Directory_KeyUp"
    IsTextSearchEnabled="True"
    IsEditable="True"
    Text="{Binding Path=CurrentDirectory, Mode=TwoWay}"
    ItemsSource="{Binding Source={x:Static self:Properties.Settings.Default}, 
        Path=DirectoryList, Mode=TwoWay}" />

The handler is:


private void Directory_KeyUp(object sender, KeyEventArgs e)
{
    ComboBox box = sender as ComboBox;
    if (box.IsDropDownOpen &&  (e.Key == Key.Delete))
    {
        TrimCombobox("DirectoryList", box.HighlightedItem);  // won't compile!
    }
}

When using the debugger, I can see box.HighlightedItem has the value I want but when I try and put in that code, it fails to compile with:

System.Windows.Controls.ComboBox' does not contain a definition for 'HighlightedItem'...

So: how do I access that value? Keep in mind that the item has not been selected. It is merely highlighted as the mouse hovers over it.

Thanks for your help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf-controls