Accessing a button click of User Control in Parent page UWP
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
add a comment |
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
add a comment |
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
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
c# .net user-controls uwp-xaml
asked Nov 21 '18 at 14:32
MadMax
51
51
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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” ...
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 '18 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 '18 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 '18 at 4:40
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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” ...
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 '18 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 '18 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 '18 at 4:40
add a comment |
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” ...
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 '18 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 '18 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 '18 at 4:40
add a comment |
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” ...
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” ...
edited Nov 22 '18 at 5:47
answered Nov 21 '18 at 15:46
kennyzx
9,86542264
9,86542264
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 '18 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 '18 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 '18 at 4:40
add a comment |
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 4:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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