Share via

WPF ListViewItem IsSelected is on Window_Closing allways false?!

Rosenbohm, Mario 40 Reputation points
2026-04-20T05:08:57.4133333+00:00

Hello,

on my ListView is the ListViewItem-property "IsSelected" binding to object-property.

    <Window.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListBoxItem">
            <Setter Property="Width" Value="250" />
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </Window.Resources>

On program is running works all fine, i can read and set the object property "IsSelected" and ListView display them.
I want to save the selected state by "before closing window" and load on program restart.

But on event Windows_Closing are all items unselected, IsSelected == false?

Why?

Best regards Mario

Developer technologies | XAML
Developer technologies | XAML

A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.


2 answers

Sort by: Most helpful
  1. Varsha Dundigalla(INFOSYS LIMITED) 4,945 Reputation points Microsoft External Staff
    2026-04-20T09:01:48.9466667+00:00

    Thank you for reaching out.
    When a WPF window starts closing, WPF first begins cleaning up the UI. This includes the visual items inside the ListView. Because of that, the ListViewItem containers start losing their selected state before the Window_Closing event runs. So when you check ListViewItem.IsSelected inside Window_Closing, it may already be reset to false, even though the user had items selected just before closing.

    This is why reading selection from the UI at that moment is not reliable.

    To save and restore selection correctly, don’t rely on ListViewItem.IsSelected during Window_Closing. Instead, store the selected state in your data model (your IsSelected property) while the application is still running normally, such as when selection changes or before closing starts. When the app starts again, load those saved IsSelected values back into your data objects, and the ListView will automatically show the same selection through data binding.

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    2 people found this answer helpful.

  2. Q&A Assist
    2026-04-20T05:09:09.5766667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    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:

    1. Bind selection to a property on the data item (as already done with IsSelected), and
    2. 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:

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.