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 6150565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:37:58+00:00 2026-05-23T19:37:58+00:00

Ok, my program in a nutshell has a list of customers. Those customers are

  • 0

Ok, my program in a nutshell has a list of customers. Those customers are all listed in a listbox so when one is clicked on all of their information appears on the form. This works through databinding, all of the controls on the page are bound to the listbox’s selectedItem.

What I would like to do now is have a message dialog that asks if the user would like to save when they try to change the selection. If they don’t I want to revert it back to the original item in the collection. If they hit cancel I want the selection to focus back on the previously selected item. I am wondering what the best way would be to accomplish this in an MVVM manner?

Currently I have a Model for my customer and my VM fills a collection of Customers that the listbox is bound to. So is there a way to handle the selection changed event on the VM that would include being able to manipulate the selectedIndex of the listbox?
Here is my code so you can see what I am doing.

                if (value != _selectedAccount)
                {
                    MessageBoxResult mbr = MessageBox.Show("Do you want to save your work?", "Save", MessageBoxButton.YesNoCancel);
                    if (mbr == MessageBoxResult.Yes)
                    {
                        //Code to update corporate
                        Update_Corporate();
                        _preSelectedAccount = value;
                        _selectedAccount = value;
                    }
                    if (mbr == MessageBoxResult.No)
                    {
                        //Do Stuff

                    }
                    if (mbr == MessageBoxResult.Cancel)
                    {

                        SelectedAccount = _preSelectedAccount;
                        NotifyPropertyChanged("SelectedAccount");
                    }

                }
  • 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-05-23T19:37:59+00:00Added an answer on May 23, 2026 at 7:37 pm

    XAML:

    <ListBox ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}" DisplayMemberPath="CustomerName"/>
    

    ViewModel:

    private Customer selectedCustomer;
    public Customer SelectedCustomer
    {
      get
      {
        return selectedCustomer;
      }
      set
      {
        if (value != selectedCustomer)
        {
          var originalValue = selectedCustomer;
          selectedCustomer = value;
          dlgConfirm dlg = new dlgConfirm();
          var result = dlg.ShowDialog();
          if (!result.HasValue && result.Value)
          {
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    selectedCustomerr = originalValue;
                    OnPropertyChanged("SelectedCustomer");
                }),
                System.Windows.Threading.DispatcherPriority.ContextIdle,
                null
            );
          }
          else
            OnPropertyChanged("SelectedCustomer");
        }
      }
    }
    

    Taken/further info from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program has a handful of classes, and 2 forms. My first form Main
Program has more than one entry point defined: 'Class.Main()'. Compile with /main to specify
In a nutshell what my program does is: it executes and takes user input
I've got a program that in a nutshell reads values from a SQL database
Nutshell: How can I disable the auto-scroll back to the top of a ListBox
Program I'm making has a simple configuration file looking something like this. @overlays =
/* Program that reads a sequence of words from keyboard and prints the list
A program I am working on right now has to generate a file. Is
I'm writing program in C# for converting between model formats. The model format has
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan

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.