title: How to close a window from the user control inside?
body: <p>I've got a WPF program and at some point I open a window:</p>
  
  <pre><code>    public object testCommand()
      {
          Window window = new Window
          {
              Title = "My User Control Dialog",
              Content = new OKMessageBox("Error Message"),
              SizeToContent = SizeToContent.WidthAndHeight,
              ResizeMode = ResizeMode.NoResize
          };
          window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
          window.ShowDialog();
  
          return null;
      }
  </code></pre>
  
  <p>User Control XAML:</p>
  
  <pre><code>&lt;UserControl x:Class="SolidX.Base.MessageBoxes.OKMessageBox"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:SolidX.Base.MessageBoxes"
               xmlns:viewProperties="clr-namespace:AppResources.Properties;assembly=AppResources"
               mc:Ignorable="d" 
               d:DesignHeight="150" d:DesignWidth="400"&gt;
      &lt;Grid&gt;
          &lt;TextBlock x:Name="textMessage" Margin="10"/&gt;
          &lt;Grid Height="Auto" VerticalAlignment="Bottom"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition/&gt;
                  &lt;RowDefinition/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;CheckBox Content="Don't show this again" HorizontalAlignment="Right" Margin="5,5,10,5"/&gt;
              &lt;StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"&gt;
                  &lt;Button x:Name="btnOk" Content="{x:Static viewProperties:Resources.Common_OK}" Height="25" VerticalAlignment="Center" Width="90" Margin="5"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>User Control Code-behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace SolidX.Base.MessageBoxes
  {
      /// &lt;summary&gt;
      /// Interaction logic for OKMessageBox.xaml
      /// &lt;/summary&gt;
      public partial class OKMessageBox : UserControl
      {
          public OKMessageBox(string Message)
          {
              InitializeComponent();
              textMessage.Text= Message;
          }
      }
  }
  </code></pre>
  
  <p>And in my user control, I am trying to have buttons that close the window (currently, setting <code>Button.IsCancel = true;</code> works, but I want this to be a reusable option - essentially making my own <code>this.Close;</code>).</p>
  
  <p>I tried this (as suggested by a couple other StackOverflow answers) in the code-behind for my usercontrol but to no avail (<code>parentWindow</code> is always null):</p>
  
  <pre><code>var parentWindow = Window.GetWindow(this);
  parentWindow.Close();
  </code></pre>