Search code examples
c#wpfuser-controls

How to close a window from the user control inside?


I've got a WPF program and at some point I open a window:

    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;
    }

User Control XAML:

<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">
    <Grid>
        <TextBlock x:Name="textMessage" Margin="10"/>
        <Grid Height="Auto" VerticalAlignment="Bottom">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <CheckBox Content="Don't show this again" HorizontalAlignment="Right" Margin="5,5,10,5"/>
            <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="btnOk" Content="{x:Static viewProperties:Resources.Common_OK}" Height="25" VerticalAlignment="Center" Width="90" Margin="5"/>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>

User Control Code-behind:

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
{
    /// <summary>
    /// Interaction logic for OKMessageBox.xaml
    /// </summary>
    public partial class OKMessageBox : UserControl
    {
        public OKMessageBox(string Message)
        {
            InitializeComponent();
            textMessage.Text= Message;
        }
    }
}

And in my user control, I am trying to have buttons that close the window (currently, setting Button.IsCancel = true; works, but I want this to be a reusable option - essentially making my own this.Close;).

I tried this (as suggested by a couple other StackOverflow answers) in the code-behind for my usercontrol but to no avail (parentWindow is always null):

var parentWindow = Window.GetWindow(this);
parentWindow.Close();

Solution

  • Try this in the UserControl:

    xaml

    <UserControl .... Loaded="UserControl_Loaded">
        <!--  -->
    </UserControl>
    

    cs

    public UserControl()
    {
        InitializeComponent();
    }
    
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        var window = Window.GetWindow(this);
        window.Close();
    }