none
How to close window from user control

    Question

  • Hello
     I have a child window opened from a mainwindow, and it is drawn by my defined user control.  The user control has two buttons, cancel and save.  I am trying to close the child window when the cancel button is pressed.  As the button is defined in another class to the window I am trying to find a better way to reference the window (to call the close method).

    So I have
    a) Settings : Window =  (Settings.xaml) which has a user control UCSettings.xaml in it.
    b) The class behind UCSettings has the button click event defined in it.  When I add the code below - it all works as expected.

    I am not sure if
    a) I should register the window (thought that was to allow xaml to access stuff defined in code)
    b) I can get to this via the parent property of the usercontrol instance (but the child window is not the parent directly)
    c) I should be raising an event ---

    Any help appreciated.....thanks

    There has to be a better way........


     
     
    // This code is defined in the class that is my user control definition  
     
    private void SettingsCancelButton_Click(object sender, RoutedEventArgs e)  
    {  
         e.Handled = true;  
         if ( MessageBox.Show("Are you sure you want to cancel?""Cancel?",  
                    MessageBoxButton.YesNo , MessageBoxImage.Question) == MessageBoxResult.Yes)   
         {  
     // find the window that is holding this user control....and then call close on it....  
            foreach (Window window in Application.Current.Windows)    
            {  
                if (window.Title == "Settings")   //could also compare types here i guess.....  
                   {  
                        window.Close();  
                        break;  
                    }  
            }  
    }  
     
     
    Tuesday, October 14, 2008 5:08 AM

Answers

  • I am not sure if there is a direct way. For my projects I have a BaseUserControlClass that has a 'ParentWindow' DependencyProperty that can be set from XAML or code
        public class UCBase : UserControl 
        { 
            private static DependencyProperty ParentWindowProperty; 
     
            //The parent window 
            public Window ParentWindow 
            { 
                set { SetValue(ParentWindowProperty, value);} 
                get { return (Window)GetValue(ParentWindowProperty); } 
            } 
     
            static UCBase() 
            { 
                FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(); 
                ParentWindowProperty = DependencyProperty.Register("ParentWindow", typeof(Window), typeof(UCBase), metadata, null); 
            } 
         } 


    Then I set it from XAML by binding to the ancestor type of Window or from code when you instantiate by myUserControl.ParentWindow = this;
    Then when I want to close the hosting window, I just call this.ParentWindow.Close(); from inside the usercontrol OR myUserControl.ParentWindow.Close();
    Works anyway you want!

    It might be not the best way, but it works! I use it all the time.  OH of course you can just add the DependencyProperty Directly to your UserControl, if you don't use it a lot!

    Hope this helps.

    -noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, October 15, 2008 10:13 PM
    • Edited by noorbakhsh Wednesday, October 15, 2008 10:26 PM
    • Marked as answer by Jim Zhou - MSFT Monday, October 20, 2008 9:53 AM
    Wednesday, October 15, 2008 10:07 PM

All replies

  • well -

    I tried raising an event and that works, created a routed event and a handler, and set it to bubble up to the window,

    and its a lot of code  -still its got to be nicer and better performance,

    thanks for reading - its non trivial i guess at some level (or too trivial?)
    Tuesday, October 14, 2008 6:18 AM
  • Hi,

    I should try another solution, the command will work but this seams like a simple solution:
    Use the static method on the Window class to get the Window of a specific object.

    Something like this:

    Window window = Window.GetWindow(this);  
    if (window != null)  
        window.Close(); 

    You can set the DialogResult property of the Window if needed.
    • Proposed as answer by KP_SES Friday, March 28, 2014 6:35 PM
    Wednesday, October 15, 2008 7:40 PM
  • I am not sure if there is a direct way. For my projects I have a BaseUserControlClass that has a 'ParentWindow' DependencyProperty that can be set from XAML or code
        public class UCBase : UserControl 
        { 
            private static DependencyProperty ParentWindowProperty; 
     
            //The parent window 
            public Window ParentWindow 
            { 
                set { SetValue(ParentWindowProperty, value);} 
                get { return (Window)GetValue(ParentWindowProperty); } 
            } 
     
            static UCBase() 
            { 
                FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(); 
                ParentWindowProperty = DependencyProperty.Register("ParentWindow", typeof(Window), typeof(UCBase), metadata, null); 
            } 
         } 


    Then I set it from XAML by binding to the ancestor type of Window or from code when you instantiate by myUserControl.ParentWindow = this;
    Then when I want to close the hosting window, I just call this.ParentWindow.Close(); from inside the usercontrol OR myUserControl.ParentWindow.Close();
    Works anyway you want!

    It might be not the best way, but it works! I use it all the time.  OH of course you can just add the DependencyProperty Directly to your UserControl, if you don't use it a lot!

    Hope this helps.

    -noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, October 15, 2008 10:13 PM
    • Edited by noorbakhsh Wednesday, October 15, 2008 10:26 PM
    • Marked as answer by Jim Zhou - MSFT Monday, October 20, 2008 9:53 AM
    Wednesday, October 15, 2008 10:07 PM
  • Hello
     I have a child window opened from a mainwindow, and it is drawn by my defined user control.  The user control has two buttons, cancel and save.  I am trying to close the child window when the cancel button is pressed.  As the button is defined in another class to the window I am trying to find a better way to reference the window (to call the close method).

    So I have
    a) Settings : Window =  (Settings.xaml) which has a user control UCSettings.xaml in it.
    b) The class behind UCSettings has the button click event defined in it.  When I add the code below - it all works as expected.

    I am not sure if
    a) I should register the window (thought that was to allow xaml to access stuff defined in code)
    b) I can get to this via the parent property of the usercontrol instance (but the child window is not the parent directly)
    c) I should be raising an event ---

    Any help appreciated.....thanks

    There has to be a better way........


     
     
    // This code is defined in the class that is my user control definition  
     
    private void SettingsCancelButton_Click(object sender, RoutedEventArgs e)  
    {  
         e.Handled = true;  
         if ( MessageBox.Show("Are you sure you want to cancel?""Cancel?",  
                    MessageBoxButton.YesNo , MessageBoxImage.Question) == MessageBoxResult.Yes)   
         {  
     // find the window that is holding this user control....and then call close on it....  
            foreach (Window window in Application.Current.Windows)    
            {  
                if (window.Title == "Settings")   //could also compare types here i guess.....  
                   {  
                        window.Close();  
                        break;  
                    }  
            }  
    }  
     
     

    if  (window.IsActive)

    {

           window.Close();

    }


    • Edited by Negm123 Sunday, November 27, 2011 3:18 PM
    Sunday, November 27, 2011 3:17 PM