A: 

The definition of System.Windows.Controls.ComboBox does not contain a property HighlightedItem - that's why you're code does not compile.

Are you using a combo box derived from System.Windows.Controls.ComboBox? Then just cast it to the appropriate type.

Later note: If you want to catch the highlighted event of a ComboBox read this link - it addresses exactly this issue.

Ando
What would that type be? And why does the debugger show HighlightedItem?
Harold Bamford
Don't know what you are looking at in the debugger - can you copy/paste what variable you are watching and what type you get for that variable?
Ando
Picture added to the original post. This is what I am looking for.
Harold Bamford
One solution to your issue is to catch the highlight event - I edited my post, see the link there for more information
Ando
Alas, that seems to be for Windows.Forms rather than WPF. Thanks anyway
Harold Bamford
A: 

You can create your own DrawItem Event handler and save the index of the items as they are actively being drawn and keep the DrawItemState.Selected (ie. the highlighted) one.

void Main()
{
    Application.Run(new Form1());
}

public partial class Form1 : Form
{
    ComboBox ComboBox1;
    string[] ds = new string[]{"Foo", "Bar", "Baz"};

    public Form1 ()
    {
        InitializeComboBox();
    }

    private void InitializeComboBox()
    {
        ComboBox1 = new ComboBox();

        ComboBox1.DataSource = ds;
        Controls.Add(ComboBox1);

        ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        ComboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
    }

    private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.Graphics.DrawString(ds[e.Index],ComboBox1.Font,Brushes.Black,e.Bounds);

        if (e.State == DrawItemState.Selected)
        {
            //stores the "HighlightedIndex" in the control's tag field.  Change as you see fit.
            ComboBox1.Tag = e.Index; 
            //Console.WriteLine(e.Index);
        }
    }
}
Martin Neal
Actually I'm not using Forms but rather WPF 3.5
Harold Bamford
+1  A: 

The Highlighted Item property is a Non-Public member, so you can't call it from another class.

alt text

I believe you need to use Reflection to get at Non-Public members. Here's a SO post on the subject: http://stackoverflow.com/questions/769634/access-non-public-members-reflectionattribute

Jean Azzopardi
That seems to have done it. Thanks! I will put the final code in a separate answer but credit you with the "winner."
Harold Bamford
Jean, how do you get the Watch window to separate out "Non-Public members"? I've tried this in VS2008 and VS2010 (Pro edition) and it doesn't do that. If it had, I might have figure out the problem much sooner.
Harold Bamford
I mouse over the item I want to see, then go to Raw View, then base, then Non Public Members is usually shown. Sometimes there are multiple levels of (base) if you are using inheritance.I am using VS 2010 Ultimate (Still using free RC while it lasts ;)), but I think it should be in Pro.
Jean Azzopardi
No Raw View for me. Must be for Ultimate and I ain't got $11K to spend on it! Thanks, anyway.
Harold Bamford
FYI I think you can still get the RC for free, but it will expire in the coming months.Personally, I'd ask a question to SO whether Raw View is only available in 2010 Ultimate. With the thousands of SO users using multiple versions of Visual Studio, you should get an answer. Also, check your Debug Settings.
Jean Azzopardi
I tried a new solution with Visual Studio 2008 Pro Edition, I was able to see the Non-Public members of a form by hovering the mouse over "this", and then clicking on base. Non Public Members was at the bottom of the list.So I guess Raw View might be something VS/WPF related. I don't think it's an Ultimate only thing, though.
Jean Azzopardi
On rare occasions, I see a Raw View, but it is often empty. Thanks for your help.
Harold Bamford
+1  A: 

Below is the final code, as inspired by Jean Azzopardi's answer. The HighlightedItem that was showing up in the debugger was a non-public property and I am forcing access with a sequence of GetType().GetProperty().GetValue()

private void Directory_KeyUp(object sender, KeyEventArgs e)
{
    ComboBox box = sender as ComboBox;
    if (box.IsDropDownOpen && (e.Key == Key.Delete))
    {
        const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
        PropertyInfo hl = box.GetType().GetProperty("HighlightedItem", flags);
        if (hl != null)
        {
            String hlString = hl.GetValue(sender, null) as String;
            // TODO: remove from DirectoryList
        }
    }
}
Harold Bamford