A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
Window.Closing is raised after WPF has already cleared the selection state of the ListViewItem visuals, so the IsSelected property on the container items is no longer true when that event runs. By the time Closing executes, the ListViewItem containers are transitioning to the Unselected visual state.
To persist selection, do not rely on the ListViewItem.IsSelected value during Window_Closing. Instead:
- Bind selection to a property on the data item (as already done with
IsSelected), and - Persist that data item property at a point where it still reflects the current selection (for example, when the selection changes, or via a command invoked before the window starts closing).
Because the selection state is stored on the data objects, it remains available even after the window and its visual containers (ListViewItem) are torn down. On the next startup, restore the IsSelected values from the persisted data, and the ListView will re-select the items via the binding.
If visual styling of selected items is needed, use the SelectionStates visual states (Selected, Unselected, SelectedUnfocused) in the ListViewItem template rather than reading IsSelected during Closing.
References: