Aave
Aave

Reputation: 568

WPF: Reset ListView selected index after items refreshed

I have an ListView defined in XAML, and it's ItemsSource is set code-behind. ItemsSource is not a property, so I dont want bind it to observable collection. To update GUI I call ListView.Items.Refresh() method after selected index was changed (I do some work on selection changed and list view items are display the result). After that two situations may occurs:

My question is what may I do to make selected item index of ListView right after selected item was changed by keyboard and items were refreshed in code?

Upvotes: 1

Views: 1420

Answers (1)

Vijay
Vijay

Reputation: 673

Instead of SelectionChanged event why dont you try MouseLeftButtonDown event and KeyDown event.

This will solve your issue.

Snippet as follow,

private void lst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            item = lst.SelectedItem;
            fnTask();
        }

        private void lst_KeyDown(object sender, KeyEventArgs e)
        {
            item = lst.SelectedItem;
            fnTask();
        }
        private void fnTask()
        {
            lst.Items.Refresh();
            lst.SelectedItem = item;
        }

Upvotes: 0

Related Questions