Search code examples
wpflistboxwpf-controlsselectionchanged

How to get OnSelectionChanged ListBoxItem background change when modifying the data at the same time using a ListBox control?


I have a ListBox control with an ObservableCollection instance as the control's ItemsSource property.

Everything works fine, but when I handle the control's OnSelectionChanged, my business logic modifies the collection's data and I no longer get the ListBoxItem background change you usually get when your ListBox selection changes.

Did anyone encounter the same problem ? Any solution here ?

Thanks and best regards,

Romain


Solution

  • It sounds like you are losing your selected item reference when the list changes.

    Bind your ListBox's SelectedItem to a property on your data context and handle selection changes there.