Click here to Skip to main content
15,885,156 members
Articles / Desktop Programming / WPF

Closing Windows and Applications with WPF and MVVM

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
10 Aug 2012CPOL12 min read 222.1K   5.8K   66  
This article samples a MVVM conform implementation of startup and shutdown sequences for an application and its dialogs.
namespace DialogCloser.Behaviour
{
  using System.Windows;

  /// <summary>
  /// Attachable behaviour class that can be used to tell a View (window) to close itself
  /// when the ViewModel has determined that.
  /// 
  /// Usage in XAML and ViewModel is the following
  /// 
  /// --- ViewModel ---
  /// private this.mDialogCloseResult = null;
  /// 
  /// public bool? DialogCloseResult
  /// {
  ///   get
  ///   {
  ///     return this.mDialogCloseResult;
  ///   }
  ///
  ///   private set
  ///   {
  ///     if (this.mDialogCloseResult != value)
  ///     {
  ///       this.mDialogCloseResult = value;
  ///       this.NotifyPropertyChanged(() => this.DialogCloseResult);
  ///     }
  ///   }
  /// }
  /// 
  /// --- XAML in View ---
  ///  
  /// &lt;Window ...
  /// xmlns:xc="clr-namespace:MsgBox.Internal.ViewModel"
  /// xc:DialogCloser.DialogResult="{Binding DialogCloseResult}"&gt;
  /// 
  /// Source: http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form
  /// </summary>
  public static class DialogCloser
  {
    /// <summary>
    /// Dependency property for attached behaviour in dialog windows.
    /// This can be is used to close a dialog window via ViewModel.
    /// </summary>
    private static readonly DependencyProperty DialogResultProperty =
        DependencyProperty.RegisterAttached(
            "DialogResult",
            typeof(bool?),
            typeof(DialogCloser),
            new PropertyMetadata(DialogResultChanged));

    /// <summary>
    /// Setter of corresponding dependency property
    /// </summary>
    /// <param name="target"></param>
    /// <param name="value"></param>
    public static void SetDialogResult(Window target, bool? value)
    {
      target.SetValue(DialogResultProperty, value);
    }

    private static void DialogResultChanged(DependencyObject d,
                                            DependencyPropertyChangedEventArgs e)
    {
      var window = d as Window;

      if (window != null)
      {
        if (window.Visibility == Visibility.Visible)
        {
          // window.DialogResult = e.NewValue as bool?;
          window.Close();
        }
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed Edi

and a few other projects on GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.

https://de.linkedin.com/in/dirkbahle

Comments and Discussions