Some of the Windows Forms developers I've spoken to have said that one thing they want to learn is how to dynamically create controls in WPF and Silverlight. In this post I'll show you several different ways to create controls at runtime using Silverlight 4 and WPF 4. First, we'll start with how to create controls in XAML. From there, we'll move to dynamically-loaded XAML before we take a look at using the CLR object equivalents.
Creating Controls at Design Time in XAMLCreating controls using the design surface and/or XAML editor is definitely the easiest way to create your UI. You can use Expression Blend or Visual Studio, depending upon how creative you want to be. If you want a more dynamic layout, you can hide and show panels at runtime. Here's an example layout:<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions> <TextBlock Text="First Name"
Height="19"
Margin="0,7,31,4" />
<TextBox x:Name="FirstName"
Margin="3"
Grid.Row="0"
Grid.Column="1" />
<TextBlock Text="Last Name"
Margin="0,7,6,3"
Grid.Row="1"
Height="20" />
<TextBox x:Name="LastName"
Margin="3"
Grid.Row="1"
Grid.Column="1" />
Read more: 10REM.NET
Creating Controls at Design Time in XAMLCreating controls using the design surface and/or XAML editor is definitely the easiest way to create your UI. You can use Expression Blend or Visual Studio, depending upon how creative you want to be. If you want a more dynamic layout, you can hide and show panels at runtime. Here's an example layout:<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions> <TextBlock Text="First Name"
Height="19"
Margin="0,7,31,4" />
<TextBox x:Name="FirstName"
Margin="3"
Grid.Row="0"
Grid.Column="1" />
<TextBlock Text="Last Name"
Margin="0,7,6,3"
Grid.Row="1"
Height="20" />
<TextBox x:Name="LastName"
Margin="3"
Grid.Row="1"
Grid.Column="1" />
Read more: 10REM.NET