locked
How to get data from a popup usercontrol

    Question

  •   In Mainpage, I popup a UserControl which has Textbox and button just like a MessageDialog. After creating a new UserControl, I set it as Popup->Chiled property, then pop it up. This popup covers Mainpage. Then I can input items in TextBox, and I want to get input data. But you know Mainpage and Popup like two pages. How can I receive a input item in Mainpage from Popup?

      Thanks!

    Wednesday, September 05, 2012 9:30 AM

Answers

  • 1.- A solution consist in working thru the MainPage.DataContext pointing to the ViewModel:

    - when you instantiate a PopUp w/o more ado, you'll notice that it receives the DataContext of the MainPage [where it is instantiated]. This is also true if the PopUp is created from the MainPage.AppBar [e.g.]

    - any UserControl that belongs to that PopUp gets the same DataContext [just add a 'UserControl.Loaded' event to confirm the DataContext value]

    - the UserControl stores everything in the DataContext [which is an instance of the 'ViewModel' class, e.g.]

    - the MainPage watches the PopUp close event [add a event handler in MainPage], upon which it will retrieve all data from the DataContext

    ---

    2.- A variant of the above exists if you don't have [or don't want to use] a MainPage.DataContext referencing a ViewModel:

    - you instantiate the PopUp in MainPage [code behind]

    - you set the PopUp.DataContext to a MyBusinessObjectForPopUp singleton instance [containing all properties you want to shuttle back to the MainPage]

    - in MainPage you add a PopUp close event handler [code-behind]: when that handler is invoked, the MainPage retrieves all data from the PopUp MyBusinessObjectForPopUp singleton instance.

    ---

    Note 1: 'OK' and 'Cancel' are handled inside the 'UserControl' and set the Properties of the shuttled DataContext as you want them to be exploited by MainPage on return

    Note 2: the above means that you'll have to use <TextBox Text={Binding Path=MyDescription, Mode=TwoWay}...> XAML style [with Property Notification in the business object] in order to retrieve and store data values inside the MyBusinessObjectForPopUp [e.g.]


    • Edited by ForInfo Thursday, September 06, 2012 10:27 AM
    • Marked as answer by Jesse Jiang Wednesday, September 12, 2012 2:10 AM
    Thursday, September 06, 2012 10:23 AM

All replies

  • See if the Message dialog sample function SDKSample::MessageDialogSample::CustomCommand::CustomCommandButton_Click in CustomCommand.xaml.cpp demonstrates the behavior you are after.

    David Lamb

    Thursday, September 06, 2012 1:16 AM
    Moderator
  • Thanks DavidLamb for your response. Actually I don't want to implement a Message dialog. My purpose is put a UserControl which has TextBox and other contorls in a Popup.Like this:

    And I want to receive data from this popup. Here is code sinpped:

    MainPage.xaml

    <Page>
        <Grid>     
           <Popup x:Name="AddTaskPopup">
                <local:CreateTaskPopup/>
            </Popup>
        </Grid>
    </Page>

    MainPage.xaml.cpp

    void AddTaskPopupFunc()
    {
    	AddTaskPopup->IsOpen = true;
    	AddTaskPopup->Closed += ref new EventHandler<Object^>(this,&AddDayTask::Popup_Closed);
    }

    UserControl as Popup's child

    <UserControl>
        
    <Grid>
     <TextBlock x:Name="TaskName"/>
     <TextBox x:Name="TaskInput"/>
     <TextBlock x:Name="RelName"/>
     <TextBox x:Name="RelInput"/>
     <TextBlock x:Name="DescriptionName"/>
     <TextBox x:Name="DescriptionInput"/>
     <Button x:Name="OK"/>
     <Button x:Name="Cancel"/>
    </Grid>
    
    </UserControl>
    
    I just want to get input from UserControl to MainPage. How should I do?

    Thursday, September 06, 2012 3:09 AM
  • 1.- A solution consist in working thru the MainPage.DataContext pointing to the ViewModel:

    - when you instantiate a PopUp w/o more ado, you'll notice that it receives the DataContext of the MainPage [where it is instantiated]. This is also true if the PopUp is created from the MainPage.AppBar [e.g.]

    - any UserControl that belongs to that PopUp gets the same DataContext [just add a 'UserControl.Loaded' event to confirm the DataContext value]

    - the UserControl stores everything in the DataContext [which is an instance of the 'ViewModel' class, e.g.]

    - the MainPage watches the PopUp close event [add a event handler in MainPage], upon which it will retrieve all data from the DataContext

    ---

    2.- A variant of the above exists if you don't have [or don't want to use] a MainPage.DataContext referencing a ViewModel:

    - you instantiate the PopUp in MainPage [code behind]

    - you set the PopUp.DataContext to a MyBusinessObjectForPopUp singleton instance [containing all properties you want to shuttle back to the MainPage]

    - in MainPage you add a PopUp close event handler [code-behind]: when that handler is invoked, the MainPage retrieves all data from the PopUp MyBusinessObjectForPopUp singleton instance.

    ---

    Note 1: 'OK' and 'Cancel' are handled inside the 'UserControl' and set the Properties of the shuttled DataContext as you want them to be exploited by MainPage on return

    Note 2: the above means that you'll have to use <TextBox Text={Binding Path=MyDescription, Mode=TwoWay}...> XAML style [with Property Notification in the business object] in order to retrieve and store data values inside the MyBusinessObjectForPopUp [e.g.]


    • Edited by ForInfo Thursday, September 06, 2012 10:27 AM
    • Marked as answer by Jesse Jiang Wednesday, September 12, 2012 2:10 AM
    Thursday, September 06, 2012 10:23 AM