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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:46:03+00:00 2026-05-26T07:46:03+00:00

In my Application i have a MainWindow that import over MEFedMVVM the ViewModel: xmlns:mefed=clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF

  • 0

In my Application i have a MainWindow that import over MEFedMVVM the ViewModel:

xmlns:mefed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
mefed:ViewModelLocator.ViewModel="MainViewModel"

And now i have my ViewModel too that realize the ViewModel:

[ExportViewModel("MainViewModel")]
public class MainViewModel: ViewModelBase

In my ViewModel i have a ICommand property for closing the window. The event for closing can comes from anywhere. And with help from the Cinch Framework 2.0 i realize a Simplecommand with Execute methode.

Question

How can i Close the Window from my execute methode? Over the dependency injection i haven’t a constructor i can’t register an event or give the view as parameter to the viewmodel.

Edit

However, a possibility which I think is not nice:

Call this in the methode

Application.Current.MainWindow.Close()
  • 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-26T07:46:03+00:00Added an answer on May 26, 2026 at 7:46 am

    You can achieve this by writing an ICommand that passes the Window instance in as a parameter.

    A good example is available here: How can I assign the 'Close on Escape-key press' behavior to all WPF windows within a project?

    In that post the ICommand is eventually bound to a KeyBinding (so that the Escape key can be used to close the window) but you would be able to bind the command to any button or invoke it from anywhere within the view. The important part is to use a RelativeSource on your command parameter binding to reference the Window that you want to close

    Edit in response to comments

    The command is a singleton, but there is no requirement for it to be – it is only a singleton because it is stateless and it makes the binding easier. It acquires a reference to the Window through binding, so for a UserControl you can just use:

    <Button Command="{x:Static mynamespace:CloseWindowCommand.Instance}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Content="Close My Parent Window" />
    

    To be able to call it from view model code is slightly more complicated and needs a different approach; a good example can be found here: http://gallery.expression.microsoft.com/WindowCloseBehavior

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

Sidebar

Related Questions

Guys, I have a basic WPF application. Contains App.xaml as always and a Mainwindow.xaml.
I have a wpf application with a simple window that shows a text in
I have a WPF application with the main Window class called MainWindow. Since I
I have a WPF (C# and .NET 4) application that has a long running
In WPF application I have MainWindow : Window which loads a POSView : UserControl
I have a new VB.Net WPF application. The MainWindow.xaml contains nothing more than a
I have an application with a main window that loads in text file(s) and
I have module application. When I run it, the main window of that app
In My application have time consuming process.There fore i try to do that operation
I have application that makes different queries with different results so the caching in

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.