none
[UWP]C# UWP: How do you close a Popup from the Popup? RRS feed

  • Question

  • I know this sounds simple, but I cant find it.

    I have created a User Control, and have a Close button on it.  I cant find the command  to use in the Close_Click  to shut the Popup down.  There is no title bar with an X  to close it.  And   Window.Current.Close();  doesn't work.

    I can do it with a second click of the Popup button on the parent

                if(popupHelp.IsOpen == true)
                    popupHelp.IsOpen = false;
                else
                    popupHelp.IsOpen = true;

    But that is a, don't want to do that where there is a nice Close button on the form.


    Saturday, September 14, 2019 6:51 PM

Answers

  • Hi,

    You could not close the UserControl itself inside of it. A simple way is that you could create a event in the UserCrontrol and subscribe the event in the page(for example, it's main page.) you want to show it. When you want to close the UserCrontrol by clicking a button inside of it, you could hand the event in the main page and remove the UserCrontrol from main page. Like this:

    UserControl:

      public sealed partial class TestControl : UserControl
        {
            //declare the event
            public event EventHandler CloseEvent;
    
            public TestControl()
            {
                this.InitializeComponent();
            }
    
            //close button clicked
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                //bubble the event up to the parent
                if (this.CloseEvent != null)
    
                    this.CloseEvent(this, new EventArgs());
            }
        }

    In the main page:

     public MainPage()
            {
                this.InitializeComponent();
                this.Loaded += MainPage_Loaded;
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                TestControl control = new TestControl();
                control.CloseEvent += new EventHandler(UserControl_CloseClick);
                RootGrid.Children.Add(control);
            }
    
            protected void UserControl_CloseClick(object sender, EventArgs e)
            {
                //handle the event  
                //The usercontrol is the second element in the root panel. So I directly removed it using index.
                RootGrid.Children.RemoveAt(1);
            }


    Best regards

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, September 16, 2019 2:22 AM
    Moderator

All replies

  • Inside the Close_Click function, find the parent popup, probably like this:

       Popup p = (Popup)this.Parent;

    Then execute p.IsOpen = false.

    But if User Control is not an immediate child of Popup, then try ‘Popup p = (Popup)this.Parent.Parent’ or execute a loop.

    Saturday, September 14, 2019 7:49 PM
  • You need to ask UWP questions in the forum for UWP. It is not called UWP, it begins with universal.




    Sam Hobbs
    SimpleSamples.Info


    • Edited by Simple Samples Sunday, September 15, 2019 5:33 AM deleted incorrect information
    Sunday, September 15, 2019 5:31 AM
  • I posted this is a C# forum, apparently the wrong place.  So a repost.

    I created a User control which is  called as a Popup from the main form.  It has on it a Quit button. I have been unable to figure out how to close it. I was thinking there would be a me.close() or the some form of application.exit().

    I can do it from the calling program, just click on the button that started the popup.

     if (popupHelp.IsOpen == true)
             popupHelp.IsOpen = false;
     else
             popupHelp.IsOpen = true;

    Someone suggested  going thru the parent to find the method and call it ssue the close.  I moved the above code to a procedure instead so it wouldn't be in a button click event.  Then the proc would have to be callable by the User control.

    Well, I am looking at that now (not getting very far), but it sure seems like a poor way to do it.  What if the User control covers the whole screen.  There has to be a way to do it.  

    Sunday, September 15, 2019 9:56 PM
  • Hi,

    You could not close the UserControl itself inside of it. A simple way is that you could create a event in the UserCrontrol and subscribe the event in the page(for example, it's main page.) you want to show it. When you want to close the UserCrontrol by clicking a button inside of it, you could hand the event in the main page and remove the UserCrontrol from main page. Like this:

    UserControl:

      public sealed partial class TestControl : UserControl
        {
            //declare the event
            public event EventHandler CloseEvent;
    
            public TestControl()
            {
                this.InitializeComponent();
            }
    
            //close button clicked
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                //bubble the event up to the parent
                if (this.CloseEvent != null)
    
                    this.CloseEvent(this, new EventArgs());
            }
        }

    In the main page:

     public MainPage()
            {
                this.InitializeComponent();
                this.Loaded += MainPage_Loaded;
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                TestControl control = new TestControl();
                control.CloseEvent += new EventHandler(UserControl_CloseClick);
                RootGrid.Children.Add(control);
            }
    
            protected void UserControl_CloseClick(object sender, EventArgs e)
            {
                //handle the event  
                //The usercontrol is the second element in the root panel. So I directly removed it using index.
                RootGrid.Children.RemoveAt(1);
            }


    Best regards

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, September 16, 2019 2:22 AM
    Moderator
  • Hi Roy

    I just implemented  your suggestions in a testApp.  It ran fine down to the remove which I have not coded yet.  So, good answer.. again.

    I am still having problems in the real app.  For some reason, in the  if (this.CloseEvent != null) statement, it is always null. Since it works in the test app, I have some hunting to do to try to figure out why.  I think this has probably been my problem all along, whatever it is.

    One more TY

    Monday, September 16, 2019 7:08 PM
  • Hum... Well. As I said, your code worked.  But

    First, I don't need this.  I am displaying the User control as the result of a button click which in turn runs the user control as a PopUp.  All I have to do is click on that button again and I can shut the Popup down.

    It should make the process easier.  The button and its click event  handling proc are already there, so we should not need to create them in code.. 

    WIth the a button coded in Xaml, I display the Control with a button click as a Popup,  it won't work anymore.  In, the User control,   If(this.UserControlCloseEvent != null)  … well it is always null, so the close event is never raised.

    If you get bored, maybe you can get farther with this than I did. Issues like this drive me nuts. But Ive spent way too much time trying to get this to work. Got to make some progress.  I have a RichEditBox issue to figure out  :-)

    Thanks

    Tuesday, September 17, 2019 1:43 AM