WPF C#: Close Window from UserControl

This code can be used to close parent window from usercontrol in WPF Window application project:

Window parent = Window.GetWindow(this);

parent.Close(); 


How does it work? Let's see on this sample project.

Here I have a WPF Window application project named WPFSample. It already has a MainWindow1.xaml in it.


I'll add a button named ButtonCall with its properties, so the code become like this:

<Window x:Class="WPFSample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WPFSample"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Button x:Name="ButtonCall" Content="Show" Width="200" Height="50" Click="ButtonCall_Click"/>

    </Grid>

</Window>


Yellow highlighted are the modifications.


<UserControl x:Class="WPFSample.UserControl1"

             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:WPFSample"

             mc:Ignorable="d" 

             Background="White"

             d:DesignHeight="350" d:DesignWidth="350">

    <Grid>

        <Button x:Name="ButtonClose" Content="Close" Width="200" 

                Height="50" Click="ButtonClose_Click" />

    </Grid>

</UserControl>


Code-behind:

I call UserControl to be displayed on a ShowDialog Window by code in MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WPFSample
{
 public partial class MainWindow : Window

 {
    public MainWindow()
    {
    InitializeComponent();
    }

    private void ButtonCall_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 ctl = new UserControl1();
        Window wd = new Window();
        DockPanel pnl = new DockPanel();
        pnl.HorizontalAlignment = HorizontalAlignment.Stretch;
        pnl.VerticalAlignment = VerticalAlignment.Stretch;
        pnl.Children.Add(ctl);
        wd.Content = pnl;
        wd.Height = 350;wd.Width = 350;
        wd.ShowDialog();
    }
 }
}

And in UserControl1.xaml I add code to close the window.

using System.Windows;
using System.Windows.Controls;

namespace WPFSample
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            Window parent = Window.GetWindow(this);
            parent.Close();

        }
    }
}

Let's try to run and debug.


Download sample project here!

Post a Comment

0 Comments