title: Close a Window programatically after a OK_Button click from a ViewModel using MVVM
body: <p>I am showing a window. The instance is created and shown within the ViewModel (bad practice I know...) </p>
  
  <pre><code>NewWindow form = new NewWindow();
  form.ShowDialog(); 
  </code></pre>
  
  <p>Within that form I have an OK_button which is doing stuff when it is pressed. There exist a ViewModel to this form which has the OK Command from the OK_Button.
  After that button is pressed doing stuff I want to close that form programatically from within the viewmodel. How can I do that?</p>
  
  <p>I use <strong>WPF</strong></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>now lets see what I do wrong: Here the DataContext event is not fired although my Window with the ViewModel is shown!?</p>
  
  <p>The window that is shown and must be closed from the ViewModel:</p>
  
  <pre><code>public partial class NewSchoolYearWindow : Window
      {
          public NewSchoolYearWindow()
          {
              InitializeComponent();
          }
  
          private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
          {
              NewSchoolYearViewModel vm = (NewSchoolYearViewModel)e.NewValue;
              vm.CloseNewSchoolYearDialog += () =&gt; this.Close();              
          }
      }
  </code></pre>
  
  <p>Why is the DataContextChanged even not fired? </p>
  
  <p>I use this XAML in my Window:</p>
  
  <pre><code>&lt;Window x:Class="TBM.View.NewSchoolYearWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:ViewModel="clr-namespace:TBM.ViewModel"
          Title="Start a new school year"
          Height="412" Width="505" 
          WindowStartupLocation="CenterScreen"        
          WindowStyle="ThreeDBorderWindow"
          ResizeMode="CanResize" DataContextChanged="Window_DataContextChanged"&gt;
      &lt;Window.Resources&gt;
  
          &lt;ViewModel:NewSchoolYearViewModel x:Key="NewSchoolYearViewModelID" /&gt;
  
      &lt;/Window.Resources&gt;
  
      &lt;Grid DataContext="{Binding ., Source={StaticResource NewSchoolYearViewModelID}}" Name="MainGrid"&gt;
          &lt;TextBlock Height="27" HorizontalAlignment="Left" Margin="68,46,0,0" Name="textBlock1" Text="School year start" VerticalAlignment="Top" Width="98" /&gt;
          &lt;TextBlock Height="27" HorizontalAlignment="Left" Margin="68,93,0,0" Name="textBlock2" Text="School year end" VerticalAlignment="Top" Width="98" /&gt;
          &lt;TextBlock Height="27" HorizontalAlignment="Left" Margin="68,169,0,0" Name="textBlock4" Text="Database name:" VerticalAlignment="Top" Width="150" TextAlignment="Left" TextTrimming="CharacterEllipsis" /&gt;
          &lt;TextBlock Height="27" HorizontalAlignment="Left" Margin="68,215,0,0" Name="textBlock3" Text="Directory:" VerticalAlignment="Top" Width="63" TextAlignment="Left" TextTrimming="CharacterEllipsis" /&gt;
          &lt;TextBox IsReadOnly="True" Text="{Binding CurrentSchoolYear.Directory}"  Height="23" HorizontalAlignment="Left" Margin="172,212,0,0" Name="textBox3" VerticalAlignment="Top" Width="224" /&gt;
          &lt;Button Command="{Binding OpenNewSchoolYearDialogCommand}" Content="DIR" Height="23" HorizontalAlignment="Right" Margin="0,211,27,0" Name="button1" VerticalAlignment="Top" Width="54" /&gt;
          &lt;Button Command="{Binding CreateNewSchoolYearCommand}" Content="OK" Height="23" HorizontalAlignment="Left" Margin="381,299,0,0" Name="button2" VerticalAlignment="Top" Width="75" /&gt;
          &lt;Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="300,299,0,0" Name="button3" VerticalAlignment="Top" Width="75" /&gt;
          &lt;DatePicker Height="25" HorizontalAlignment="Left" Margin="172,42,0,0" SelectedDate="{Binding CurrentSchoolYear.Start}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" /&gt;
          &lt;DatePicker Height="25" HorizontalAlignment="Left" Margin="172,89,0,0" SelectedDate="{Binding CurrentSchoolYear.End}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" /&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="172,166,0,0" Name="textBox1" Text="{Binding CurrentSchoolYear.Name}" VerticalAlignment="Top" Width="175" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>