Silverlight 4: How to programmatically position a control within a Grid
1 min read
You need to use the SetValue method of the DependencyObject class… I’ll let the code speak for itself:
<UserControl x:Class="SetGridProgrammatically.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition Height="93*" /> <RowDefinition Height="70*" /> <RowDefinition Height="137*" /> Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="70*" /> <ColumnDefinition Width="182*" /> <ColumnDefinition Width="148*" /> Grid.ColumnDefinitions> Grid>UserControl>
```text```csharp
Button b = new Button();
b.Content = "Silverlight";LayoutRoot.Children.Add(b);
b.SetValue(Grid.RowProperty, 1);b.SetValue(Grid.ColumnProperty, 1);![]()
More about Attached Properties here: http://msdn.microsoft.com/en-us/library/cc265152(VS.95).aspx
Share: