Jonathan
Jonathan

Reputation:

Removing an item from a listbox leaves the listbox with no selected item

I have a WPF listbox, with some custom item inside.

When user wants to deleta on of the item, what happens is that he has to reselect manually in the list rigth after delete because the list "seems" to lose focus OR no selected item exists.

Any idea ?

Thanks Jonathan

Upvotes: 0

Views: 1111

Answers (2)

user3200321
user3200321

Reputation: 11

I was struggling with that problem too (hence this edit). The solution was to store the deleted index and save it for later. A bit of a hack, but the best I have found so far:

<Window x:Class="KeepFocusAfterDelete.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="200">
    <Grid>
        <ListBox KeyUp="ListBox_KeyUp">
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
        </ListBox>
    </Grid>
</Window>

and the code is

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace KeepFocusAfterDelete
{
    public partial class MainWindow : Window
    {
        int prev = -1;
        public MainWindow() { InitializeComponent(); }
        private void ListBox_KeyUp(object sender, KeyEventArgs e)
        {
            var list = sender as ListBox;
            switch (e.Key)
            {
                case Key.Delete:
                    prev = list.SelectedIndex;
                    var items = list.SelectedItems.Cast<object>().ToList();
                    foreach (var item in items) list.Items.Remove(item);
                    prev = list.Items.Count > prev ? prev : list.Items.Count - 1;
                    list.SelectedIndex = prev;
                    break;
                case Key.Up:
                    if (-1 != prev)
                    {
                        list.SelectedIndex = prev - 1;
                        prev = -1;
                    }
                    break;
                case Key.Down:
                    if (-1 != prev)
                    {
                        prev = list.Items.Count > prev + 1 ? prev + 1: list.Items.Count - 1; 
                        list.SelectedIndex = prev;
                        prev = -1;
                    }
                    break;
            }
        }
    }
}

Select item "2", press Delete, and then Arrow Down. The selected item quickly changes from item "1" to item "4".

Thanks, -Ole

Upvotes: 1

Donnelle
Donnelle

Reputation: 5719

We always handle this by setting the selected item in code. If it was the last item in the list, make the selected index the last item. Otherwise we make it the one after the one that was deleted.

if (SnippetsList.Items.Count > index) 
    SnippetsList.SelectedIndex = index;
else 
    SnippetsList.SelectedIndex = SnippetsList.Items.Count - 1;

Upvotes: 1

Related Questions