0

I have just upgraded a project from .NET 3.5 to 4.0 and have found that ListBoxes behave differently. Items are sometimes added to ListBoxes from the code behind using

this.myListBox.SelectedItem = new MyItemType();

This no longer fires the myListBox.SelectionChanged event and is causing bugs in the program. Is there a better way of doing this?

Thanks, Matt

Coder1095
  • 828
  • 10
  • 25
  • 4
    Afaik this has never been a valid way to add items to a ListBox/ItemsControl in WPF. When setting, the `SelectedItem` should always be an item that is already contained in the Items collection (or compares equal to an existing item). – Clemens Nov 08 '12 at 11:35
  • 1
    I agree with @Clemens Add item to items list and then select it. It's a wonder this worked before... – dodsky Nov 08 '12 at 11:42
  • have yourself a look at this answer. http://stackoverflow.com/a/13121798/345189 ICollectionView.MoveCurrentTo(xxx) is your friend. – menty Nov 08 '12 at 11:55

0 Answers0