Search code examples
windows-phone-7user-controlsparameter-passing

Pass value from user control (popup) to current page windows phone 7


From Mainpage, I use this to open the user control Login.xaml :

myPopup = new Popup() { IsOpen = true, Child = new Login() };

I want to enter information into the Login user control and pass that value back to Mainpage to process, how can I do this? I know how to use OnNavigateTo and From to pass value to other page but it won't work on this case because the Popup actually lays on top the MainPage, so it does not navigate anywhere.


Solution

  • You need to use events for that:

    var login = new Login();
    login.Completed += (a, b) =>
        {
            // retrieve login.Data
            // close popup
        };
    myPopup = new Popup() { IsOpen = true, Child = login };
    

    and login user control:

    {
        ...
        public event EventHandler Completed;
        void OnCompleted
        {
            var h = Completed;
            if (h!= null) h(this, new EventArgs());
        }
    }
    

    remember to call OnCompleted() in the login whereever it suits (e.g. OK button handler)