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

  • Home
  • SEARCH
  • 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 9157531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:02:29+00:00 2026-06-17T13:02:29+00:00

I am using WPF and MVVM Light framework (I am new in using them).

  • 0

I am using WPF and MVVM Light framework (I am new in using them).

I want to do the following:

  1. When the user click on the ‘X’ close button, I want to display a confirmation window if whether he wants to exit the application or not.
  2. If yes, the application closes
  3. If no, nothing happens and he can still use the application as per normal

So far, I have this:

  • In MainWindow.xaml.cs:

    public MainWindow()
    {
        InitializeComponent();
        Closing += (s, e) => ViewModelLocator.Cleanup();
    }
    
  • In ViewModelLocator.cs:

    public static void Cleanup()
    {
        ServiceLocator.Current.GetInstance<MainViewModel>().Cleanup();
    }
    
  • In MainViewModel.cs:

    public override void Cleanup()
    {
        MessageBoxResult result = MessageBox.Show(
                        "Unsaved data will be lost, would you like to exit?",
                        "Confirmation",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Question);
    
        if (result == MessageBoxResult.Yes)
        {
          // clean-up resources and exit
        }
        else
        {
          // ????
        }
    

Actually, if the user answers ‘Yes’ or ‘No’, in both cases the application will exit.

I am not too sure how to proceed from here…

Any help would be great!

Thanks

  • 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-17T13:02:31+00:00Added an answer on June 17, 2026 at 1:02 pm

    You can use an EventToCommand in an EventTrigger to catch the closing event and set the Cancel property of the passed CancelEventArgs to true if you want to cancel the closing:

    XAML:

    <Window ...
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
       xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
       DataContext="{Binding Main, Source={StaticResource Locator}}">
       <i:Interaction.Triggers>
          <i:EventTrigger EventName="Closing">
             <cmd:EventToCommand Command="{Binding OnClosingCommand}" 
                PassEventArgsToCommand="True"/>
          </i:EventTrigger>
       </i:Interaction.Triggers>
       <Grid>
         ...
       </Grid>
    </Window>
    

    ViewModel:

    public class MainViewModel : ViewModelBase
    {
       public RelayCommand<CancelEventArgs> OnClosingCommand { get; set; }
    
       public MainViewModel()
       {
          this.OnClosingCommand = 
             new RelayCommand<CancelEventArgs>(this.OnClosingCommandExecuted);
       }
    
       private void OnClosingCommandExecuted(CancelEventArgs cancelEventArgs)
       {
          ...
    
          if (mustCancelClosing)
          {
             cancelEventArgs.Cancel = true;
          } 
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an WPF application using the mvvm-light framework. I'm new to both
I'm new to Prism, but I have successfully built several WPF/Mvvm-Light applications. I'm using
How do communicate data between wcf and wpf using entity framework 4, mvvm light?
I'm doing WPF(MVVM) project using mvvm-light libraries, and have such problem: I want to
I have a WPF/C# application that is using the MVVM light framework. When it
I am using MVVM Light toolkit in my WPF application. I would like to
I am using the MVVM light toolkit for a wpf application which talks to
Using MVVM Light, I have two WPF applications that reference a common Views library.
I am creating a project using WPF and MVVM-Light library from GalaSoft. I will
I use C#, MVVM, WPF and Resharper. When using the following code: public bool

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.