views:

455

answers:

2

I have a listbox and am attempting to select and item in code. Sometime one item is highlighted, that is it is background is colored blue, but a different item has a square blue box around the it (no highlighting just an hollow outline of a box).

Am I correct in saying one is "highlighted" and one is "selected" and do I have them correctly identified?

Should this be happening... that is these 2 things being out of sync?

Thanks Cody

A: 

The item with the blue highlighted background is the SelectedItem. The item with the blue rectangle is the item that currently believes it has the focus.

Ordinarily the Focus rectangle the selected fill are found together because the selected item usually changes with a mouse click which also brings the focus to the same element. However its possible for example that code may change the selected item whilst the ListBox still has the focus. In that case the selected highlight will move the newly selected item but the focus rectangle will remain where it is. (Note to the pendatic I'm describing what appears to the user not how things actually work under the hood).

For an insight on what is going on see the ListBoxItem style in the ListBox Styles and Templates documentation.

AnthonyWJones
A: 

Just fought this issue. Although the listbox scrollviewer will auto scroll too the selected item in the listbox the first item would have focus and as you discribe keyboard interaction then operated with the first item in the list not eh selected item.

For us the fix was

            this.MyListBox.UpdateLayout(); 
            this.MyListBox.Focus();
            this.MyListBox.SelectedItem = MyObject;
            this.MyListBox.ScrollIntoView(this.MyListBox.SelectedItem);

The order of the actions seems very important.

This was using the ListBox internal scrollviewer.

rob