WPF ListBox SelectionChanged event WPF ListBox SelectionChanged event wpf wpf

WPF ListBox SelectionChanged event


The SelectionChangedEventArgs will contain which item was deselected and which item was selected. Use e.AddedItems to get the newly selected items. e.g.

var addedItems = e.AddedItems;if(addedItems.Count > 0){    var selectedItem = addedItems[0];    File_Load_List(selectedItem.ToString(), _tempList);}

This way you do not need to worry about whether the event is raised before or after the control being updated, but you do know the event args contain the correct information.


With the MessageBox call, you likely allowed the UI to update, and change the selection before your code executed.

You should be able to remove the sleep, and use

File_Load_List(LB_Playlist.SelectedItem.Content.ToString(), _tempList);


Solved. Just added Thread.Sleep(70); before try&catch.