Updating a WPF ListBox SelectedIndex

Started by
5 comments, last by Capoeirista 9 years, 5 months ago

Hey folks,

I'm getting back in to some C# development (2D editor using Monogame/WPF) and must be missing something completely obvious with ListBoxes...

When I add a level to my game project, I refresh the list box displaying the project's levels like this :


myLevelList.Items.Clear();

Guid activeLevel = myEngineController.ActiveLevel;

List<Level> projectLevels = engine.Project.Levels;
foreach( Level level in projectLevels )
{
  myLevelList.Items.Add( level.Name );
  if( level.Id == activeLevel && activeLevel != Guid.Empty )
  {
    myLevelList.SelectedIndex = myLevelList.Items.Count - 1;
  }
}

So here myLevelList is my ListBox instance.

While this works programmatically (the ListBox instance gets the correct 'SelectedValue', 'SelectedIndex'), the UI isn't updated; there is no blue selection box around the currently selected item.

Any idea what else I need to do here?

Advertisement

I haven't used WPF in a while, so this may not be correct, but the docs provide:

ListBox.SetSelected(int index, bool value)

where value = true selects the specified item.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I haven't used WPF in a while, so this may not be correct, but the docs provide:

ListBox.SetSelected(int index, bool value)

where value = true selects the specified item.

Doesn't seem to have a SetSelected method; using .NET 4.0 and the WPF ListBox implementation (http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ListBox(v=vs.100).aspx)

I've tried calls to UpdateLayout etc after updating the selected index, but without any luck.

It's odd... when I set the SelectedIndex in the ListBox instance, everything in the class looks good. The 'SelectedValue' looks right etc, it's just that the actual UI doesn't get updated (no blue box around the list item).

IIRC selection highlights are only displayed when the listbox itself has focus.I might be wrong though.

IIRC selection highlights are only displayed when the listbox itself has focus.I might be wrong though.

That's interesting, thanks!

After calling 'Focus' on the post update of the listbox the selection highlight appeared. That's somewhat annoying, I might have a look at the extended wpf toolkit and see if they have a nicer implementation.

Cheers!

The selection in WPF list boxes when the list box doesn't have focus is (by default) a light grey which is difficult to see. If you want, re-style the control to change the color of the non-focused selection box to something that is more visible (or the same color as the one when it has focus)

The selection in WPF list boxes when the list box doesn't have focus is (by default) a light grey which is difficult to see. If you want, re-style the control to change the color of the non-focused selection box to something that is more visible (or the same color as the one when it has focus)

Yeah, I had a look at overriding the default style for a ListBox.

Using the SystemColors.ControlBrushKey I was able to set the un-focused highlight to grey with a 0.5 opacity, and that seems to do the trick :)

Cheers!

This topic is closed to new replies.

Advertisement