Accessing a button click of User Control in Parent page UWP











up vote
0
down vote

favorite












I have a user control created with name as UserControl.



This user control have a label and a button.



I use this user control in one of my page(lets say Page1), but this user control is generated based on the entries in DB. For eg: I have an entry in DB which has a column with name as 'UC1'. So on page(Page1) load, I dynamically add this usercontrol on the page.



Now when the page is loaded, I want the button click to method to be implemented in Page1 code behind. Could anyone let me know how is this possible. I am facing issue as the use control is dynamically generated and since multiple instances of usercontrols can be generated in the page based on the DB entries.



In short I want to call a method in code behind of parent page for the user control that is dynamically generated inside the parent page










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a user control created with name as UserControl.



    This user control have a label and a button.



    I use this user control in one of my page(lets say Page1), but this user control is generated based on the entries in DB. For eg: I have an entry in DB which has a column with name as 'UC1'. So on page(Page1) load, I dynamically add this usercontrol on the page.



    Now when the page is loaded, I want the button click to method to be implemented in Page1 code behind. Could anyone let me know how is this possible. I am facing issue as the use control is dynamically generated and since multiple instances of usercontrols can be generated in the page based on the DB entries.



    In short I want to call a method in code behind of parent page for the user control that is dynamically generated inside the parent page










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a user control created with name as UserControl.



      This user control have a label and a button.



      I use this user control in one of my page(lets say Page1), but this user control is generated based on the entries in DB. For eg: I have an entry in DB which has a column with name as 'UC1'. So on page(Page1) load, I dynamically add this usercontrol on the page.



      Now when the page is loaded, I want the button click to method to be implemented in Page1 code behind. Could anyone let me know how is this possible. I am facing issue as the use control is dynamically generated and since multiple instances of usercontrols can be generated in the page based on the DB entries.



      In short I want to call a method in code behind of parent page for the user control that is dynamically generated inside the parent page










      share|improve this question













      I have a user control created with name as UserControl.



      This user control have a label and a button.



      I use this user control in one of my page(lets say Page1), but this user control is generated based on the entries in DB. For eg: I have an entry in DB which has a column with name as 'UC1'. So on page(Page1) load, I dynamically add this usercontrol on the page.



      Now when the page is loaded, I want the button click to method to be implemented in Page1 code behind. Could anyone let me know how is this possible. I am facing issue as the use control is dynamically generated and since multiple instances of usercontrols can be generated in the page based on the DB entries.



      In short I want to call a method in code behind of parent page for the user control that is dynamically generated inside the parent page







      c# .net user-controls uwp-xaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 14:32









      MadMax

      51




      51
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Supposed the Button is named ‘btn1’, and the TextBlock named ‘textBlock1’, you can wire up like this



          for (int i = 0; i < 3; i++)
          {
          MyUserControl c = new MyUserControl();
          c.btn1.Click += (o, args) =>
          {
          c.textBlock1.Text = DateTime.Now.ToString();
          };

          //then add the user control to the page
          this.stackPanel1.Children.Add(c);
          }


          And add an x:FieldModifier attribute to the elements so you can access them from outside.



          <Button x:Name=“btn1” x:FieldModifier=“public” ...





          share|improve this answer























          • I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
            – MadMax
            Nov 21 at 17:08










          • Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
            – MadMax
            Nov 21 at 17:37










          • @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
            – kennyzx
            Nov 22 at 4:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414363%2faccessing-a-button-click-of-user-control-in-parent-page-uwp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Supposed the Button is named ‘btn1’, and the TextBlock named ‘textBlock1’, you can wire up like this



          for (int i = 0; i < 3; i++)
          {
          MyUserControl c = new MyUserControl();
          c.btn1.Click += (o, args) =>
          {
          c.textBlock1.Text = DateTime.Now.ToString();
          };

          //then add the user control to the page
          this.stackPanel1.Children.Add(c);
          }


          And add an x:FieldModifier attribute to the elements so you can access them from outside.



          <Button x:Name=“btn1” x:FieldModifier=“public” ...





          share|improve this answer























          • I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
            – MadMax
            Nov 21 at 17:08










          • Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
            – MadMax
            Nov 21 at 17:37










          • @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
            – kennyzx
            Nov 22 at 4:40















          up vote
          0
          down vote



          accepted










          Supposed the Button is named ‘btn1’, and the TextBlock named ‘textBlock1’, you can wire up like this



          for (int i = 0; i < 3; i++)
          {
          MyUserControl c = new MyUserControl();
          c.btn1.Click += (o, args) =>
          {
          c.textBlock1.Text = DateTime.Now.ToString();
          };

          //then add the user control to the page
          this.stackPanel1.Children.Add(c);
          }


          And add an x:FieldModifier attribute to the elements so you can access them from outside.



          <Button x:Name=“btn1” x:FieldModifier=“public” ...





          share|improve this answer























          • I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
            – MadMax
            Nov 21 at 17:08










          • Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
            – MadMax
            Nov 21 at 17:37










          • @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
            – kennyzx
            Nov 22 at 4:40













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Supposed the Button is named ‘btn1’, and the TextBlock named ‘textBlock1’, you can wire up like this



          for (int i = 0; i < 3; i++)
          {
          MyUserControl c = new MyUserControl();
          c.btn1.Click += (o, args) =>
          {
          c.textBlock1.Text = DateTime.Now.ToString();
          };

          //then add the user control to the page
          this.stackPanel1.Children.Add(c);
          }


          And add an x:FieldModifier attribute to the elements so you can access them from outside.



          <Button x:Name=“btn1” x:FieldModifier=“public” ...





          share|improve this answer














          Supposed the Button is named ‘btn1’, and the TextBlock named ‘textBlock1’, you can wire up like this



          for (int i = 0; i < 3; i++)
          {
          MyUserControl c = new MyUserControl();
          c.btn1.Click += (o, args) =>
          {
          c.textBlock1.Text = DateTime.Now.ToString();
          };

          //then add the user control to the page
          this.stackPanel1.Children.Add(c);
          }


          And add an x:FieldModifier attribute to the elements so you can access them from outside.



          <Button x:Name=“btn1” x:FieldModifier=“public” ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 5:47

























          answered Nov 21 at 15:46









          kennyzx

          9,57042263




          9,57042263












          • I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
            – MadMax
            Nov 21 at 17:08










          • Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
            – MadMax
            Nov 21 at 17:37










          • @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
            – kennyzx
            Nov 22 at 4:40


















          • I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
            – MadMax
            Nov 21 at 17:08










          • Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
            – MadMax
            Nov 21 at 17:37










          • @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
            – kennyzx
            Nov 22 at 4:40
















          I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
          – MadMax
          Nov 21 at 17:08




          I am able to set the eventhandler as mentioned above after setting it as public. But the even doesnt get triggered once I click on the button. Is anything else need to be done?
          – MadMax
          Nov 21 at 17:08












          Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
          – MadMax
          Nov 21 at 17:37




          Thank you I am able to access it now. Now having triggered the button click event from parent page, how can I access the other controls defined in the usercontrol. For eg: I need to change the Text of TextBlock in the user control after the specific button was clicked.
          – MadMax
          Nov 21 at 17:37












          @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
          – kennyzx
          Nov 22 at 4:40




          @MadMax see my updated answer, I use an lambada expression to replace the event handler, in the lambda expression, you can access the specific UserControl.
          – kennyzx
          Nov 22 at 4:40


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414363%2faccessing-a-button-click-of-user-control-in-parent-page-uwp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)