It is possible to close Window1 when Window2 is open WPF?

B M-A 361 Reputation points
2022-09-17T20:09:37.72+00:00

I have an WPF application and I want to close Window1 when Window2 is open from UserControl library.
How to do ?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,786 Reputation points Microsoft Vendor
    2022-09-22T09:16:10.993+00:00

    Hi,@B M-A . Viorel-1's answer is good, I made the example below. Please let me know if there are any questions.

    Project structure:

    243809-image.png

    Add reference WpfApp2 for WpfControlLibrary1 :
    Right click on the References of WpfControlLibrary1 -> select Add References... -> check WpfApp2 and click OK.

    UserControl:

    <UserControl x:Class="WpfControlLibrary1.UserControl1"  
             ...  
                >  
        <StackPanel Background="AliceBlue">  
            <Button x:Name="window1" Content="window1" Click="window1_Click" Width="100" Height="40"/>  
            <Button x:Name="window2"  Content="window2" Click="window2_Click" Width="100" Height="40"/>  
      
        </StackPanel>  
    </UserControl>  
    

    UserControl.xaml.cs:

     public partial class UserControl1 : UserControl  
        {  
            public UserControl1()  
            {  
                InitializeComponent();  
            }  
            Window2 win2;  
            Window1 win1 ;  
            private void window1_Click(object sender, RoutedEventArgs e)  
            {  
                win1 = new Window1();  
                win1.Show();  
            }  
      
            private void window2_Click(object sender, RoutedEventArgs e)  
            {  
                win2 = new Window2();  
                win2.Show();  
                win1.Close();  
      
            }  
        }  
    

    Add reference WpfControlLibrary1 for WpfApp3 :

    Right click on the References of WpfApp3->select Add References...->check WpfControlLibrary1 and click OK.

    <Window x:Class="WpfApp3.MainWindow"  
             
      
           >  
        <Grid>  
            <uc:UserControl1 Width="300" Height="160"/>  
        </Grid>  
    </Window>  
    

    The result:
    243810-2.gif

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-09-18T08:36:30.933+00:00

    After displaying the Window2 in your User Control, try executing these lines:

    Window w1 = Window.GetWindow( this );  
    w1.Close( );  
    

    This assumes that the User Control is inside Window1.