Page 1 of 1

Dynamic Controls

Posted: Wed May 11, 2016 1:04 pm
by JohnBaljian
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);

Re: Dynamic Controls

Posted: Thu May 12, 2016 11:53 am
by rkmore
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);
        }
    }
}

Re: Dynamic Controls

Posted: Fri May 13, 2016 10:21 am
by JohnBaljian
I see thanks a lot