My WPF C# program has a listbox that holds items that the user can manipulate: changing the order, copy/paste, etc. Currently, when I select an item in the listbox, then click the move up button, the item will move up the list, but then the item is no longer highlighted or selected. So, I cannot do consecutive manipulations without reselecting the listbox item.
How can I force my listbox to retain it’s selection and highlighting?
I would bind the ListBox’s ItemsSource to an ObservableCollection. Then you can manipulate the ObservableCollection and the ListBox will be updated for you. Here’s an example:
The XAML:
and the code: