Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:52+00:00 2026-06-13T22:57:52+00:00

My WPF C# program has a listbox that holds items that the user can

  • 0

My WPF C# program has a listbox that holds items that the user can manipulate: changing the order, copy/paste, etc. Currently, when I select an item in the listbox, then click the move up button, the item will move up the list, but then the item is no longer highlighted or selected. So, I cannot do consecutive manipulations without reselecting the listbox item.

How can I force my listbox to retain it’s selection and highlighting?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:57:53+00:00Added an answer on June 13, 2026 at 10:57 pm

    I would bind the ListBox’s ItemsSource to an ObservableCollection. Then you can manipulate the ObservableCollection and the ListBox will be updated for you. Here’s an example:

    The XAML:

    <Window x:Class="ListBox.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ListBox="clr-namespace:ListBox" Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <ListBox:ListBoxViewModel />
        </Window.DataContext>
        <StackPanel>
            <Button Command="{Binding Up}">Up</Button>
            <Button Command="{Binding Down}">Down</Button>
            <ListBox Grid.Column="0" ItemsSource="{Binding Items}" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" />
        </StackPanel>
    </Window>
    

    and the code:

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    
    namespace ListBox
    {
        public class ListBoxViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public ObservableCollection<string> Items {get; private set; }
    
            private void ExecuteUp()
            {
                if (SelectedIndex == 0)
                    return;
                Items.Move(SelectedIndex, SelectedIndex - 1);
            }
            private void ExecuteDown()
            {
                if (SelectedIndex >= Items.Count - 1)
                    return;
                Items.Move(SelectedIndex, SelectedIndex + 1);
            }
    
            public ICommand Up { get; private set; }
            public ICommand Down { get; private set; }
    
            private int m_SelectedIndex = 0;
            public int SelectedIndex
            {
                get { return m_SelectedIndex; }
                set
                {
                    m_SelectedIndex = value;
                    OnPropertyChanged("SelectedIndex");
                }
            }
    
            public ListBoxViewModel()
            {
                Items = new ObservableCollection<string>() {"London", "Paris", "Berlin"};
                Up = new RelayCommand(ExecuteUp);
                Down = new RelayCommand(ExecuteDown);
            }
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wpf program in c# that has a combobox with various colors
I have a c# program using WPF. I have it that when you press
I'm writing a utility program with C# in WPF that allows users to create
I am keen to create a new program that uses WPF Ribbon with C#.
A WPF program I'm writing in C# has the following interface on the back-end:
In a C# WPF program I have a grid that I have successfully populated
I use WPF and my program has images in a DLL resource file. I
I want to make a simple program that can restrict any communication over the
I am builing a win application that has user access control against a sql
I have a pretty simple problem. When program has been started and user tries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.