This is a mirror of official site: http://jasper-net.blogspot.com/

Close window from ViewModel

| Wednesday, October 5, 2011
Although this is somewhat corny, I will dedicate for it a post. Scenario: you want to close a window from the ViewModel, without exposing the Window to it. This can be done in a simple and clean way.
(Some code removed for brevity)

HTH,
Shimmy

XAML:

<Window
  x:Name="this"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
  <i:Interaction.Triggers>
    <i:EventTrigger SourceObject="{Binding}" EventName="Closed">
      <ei:CallMethodAction
        TargetObject="{Binding ElementName=this}"
        MethodName="Close"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
<Window>

Code (from the ViewModel - DelegateCommand refers to the one of Prism's library, but it's the same idea of RelayCommand etc.):

private ICommand _SaveAndCloseCommand;
public ICommand SaveAndCloseCommand
{
  get
  {
    return _SaveAndCloseCommand ??
      (_SaveAndCloseCommand = new DelegateCommand(SaveAndClose));
  }
}

Read more: Shimmy on .NET
QR: close-window-from-viewmodel.aspx

Posted via email from Jasper-net

0 comments: