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 ?

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    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 122.6K 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.