Dynamic Controls

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
JohnBaljian
Posts: 6
Joined: Wed May 11, 2016 12:57 pm

Dynamic Controls

Postby JohnBaljian » Wed May 11, 2016 1:04 pm

How to create runtime dynamic controls such as :

TextBlock Text_Sample= new TextBlock();
Text_Sample.Text = "SAMPLE Textblock";

//Until here is ok
//Now we need something like:

this.Controls.Add(Text_Sample);

rkmore
Posts: 55
Joined: Mon Dec 07, 2015 1:53 pm

Re: Dynamic Controls

Postby rkmore » Thu May 12, 2016 11:53 am

You are almost there.

You need a place to put the control.

For a simple example add a StackPanel to your XAML and give it a name (for example).

Code: Select all

<Page
    x:Class="Application2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Application2"
    xmlns:src="using:UserControlsTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <StackPanel x:Name="stackPanel1">   
    </StackPanel>
</Page>


Then you can do exactly what you were doing like this...

Code: Select all

namespace Application2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            TextBlock myTextBlock = new TextBlock() { Text = "Hello World" };
            stackPanel1.Children.Add(myTextBlock);
        }
    }
}

JohnBaljian
Posts: 6
Joined: Wed May 11, 2016 12:57 pm

Re: Dynamic Controls

Postby JohnBaljian » Fri May 13, 2016 10:21 am

I see thanks a lot


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 4 guests