Skip to main content

I just completed a blog post on this very topic. In a nutshell, add an ActionAction property to your ViewModel with getget and setset accessors. Then define the ActionAction from your View'sView constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the ViewView constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction(); // Calls Close() method of the View 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View's constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction(); // Calls Close() method of the View 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action(this.Close); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction(); // Calls Close() method of the View 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View's constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction;CloseAction(); // Calls Close() method of the View 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View's constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction; 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View's constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction(); // Calls Close() method of the View 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.

Source Link

I just completed a blog post on this very topic. In a nutshell, add an Action property to your ViewModel with get and set accessors. Then define the Action from your View's constructor. Finally, invoke your action in the bound command that should close the window.

In the ViewModel:

public Action CloseAction { get; set;} 

and in the View constructor:

private View() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; if ( vm.CloseAction == null ) vm.CloseAction = new Action( () => this.Close() ); } 

Finally, in whatever bound command that should close the window, we can simply invoke

CloseAction; 

This worked for me, seemed like a fairly elegant solution, and saved me a bunch of coding.