diff --git a/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs b/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs index 8dfaaab07..5fbdcd504 100644 --- a/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs +++ b/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs @@ -190,17 +190,21 @@ namespace mbts.ui.controls } } - void item_Unselected(object sender, RoutedEventArgs e) { - if (_selection_changing) return; - var item = ((ListBoxItem)sender).DataContext; - var selected_items = Selection; - if (selected_items.Contains(item)) { - using (_selection_change()) { - selected_items.Remove(item); - _update_is_all_selected(); - } - } - } + void item_Unselected(object sender, RoutedEventArgs e) { + if (!_selection_changing && sender is ListBoxItem container) { + // the [ListBoxItem] sender may be "Disconnected" https://stackoverflow.com/questions/14282894/wpf-listbox-virtualization-creates-disconnecteditems/14358688 + var item = ItemContainerGenerator.ItemFromContainer(container); + if (item != DependencyProperty.UnsetValue) { + var selected_items = Selection; + if (selected_items.Contains(item)) { + using (_selection_change()) { + selected_items.Remove(item); + _update_is_all_selected(); + } + } + } + } + } void item_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { using (_selection_change()) ((ListBoxItem)sender).IsSelected = Selection.Contains(e.NewValue); @@ -368,4 +372,4 @@ namespace mbts.ui.controls } } } -} \ No newline at end of file +}