Answered by:
Add a usercontrol using c#

Question
-
I have two usercontrols already created, and I would like to add these usercontrols to the main page using only c# code, because the number of usercontrols to introduce will depende on an integer that the user will provide me.
Propably I'll have to use a gridview but I didn't find any examples that could explain it.
Is it possible to add a usercontrol to the main page using c# code, or should I create it in the XAML page and work the visibilities? (I prefer to use the c# mode because I could in some way to limit the number of user controls to introduce.)
Thanks
ACanha
Thursday, August 15, 2013 10:57 PM
Answers
-
Hello ACanha :)
yes, you can add elements using C#
and here an example code might be useful
public MainPage() { InitializeComponent(); TextBlock printTextBlock = new TextBlock(); printTextBlock.Text = "Hello, World!"; MainStackPanel.Children.Add(printTextBlock); }
there is already a stackpanel created named "MainStackPanel"
and this might be created as well, i am just giving you an example
which means if you got a gird named for example "TheGrid"
you may do like this and it will work
theGrid.Children.Add(printTextBlock);
you may also create the gird using C#
Grid myGrid = new Grid(); myGrid.Margin = new Thickness(0, 0, 0, 0);
and see where will you add your grid.
i hope this was helpfull and if there is any further thing you wanna know about, please let me know :)
- Marked as answer by ACanha Friday, August 16, 2013 11:08 PM
Friday, August 16, 2013 4:38 AM
All replies
-
Hello ACanha :)
yes, you can add elements using C#
and here an example code might be useful
public MainPage() { InitializeComponent(); TextBlock printTextBlock = new TextBlock(); printTextBlock.Text = "Hello, World!"; MainStackPanel.Children.Add(printTextBlock); }
there is already a stackpanel created named "MainStackPanel"
and this might be created as well, i am just giving you an example
which means if you got a gird named for example "TheGrid"
you may do like this and it will work
theGrid.Children.Add(printTextBlock);
you may also create the gird using C#
Grid myGrid = new Grid(); myGrid.Margin = new Thickness(0, 0, 0, 0);
and see where will you add your grid.
i hope this was helpfull and if there is any further thing you wanna know about, please let me know :)
- Marked as answer by ACanha Friday, August 16, 2013 11:08 PM
Friday, August 16, 2013 4:38 AM -
Yes, you can add usercontrol to your main page.
Here is link for add usercontrol.
Show this link. may be helpful for you:
Friday, August 16, 2013 7:34 AM -
Thanks a lot AhmedAtef07. It worked just fine.Friday, August 16, 2013 11:07 PM