Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How can I set the horizontalalignment of an individual row in grid?
2 replies. Latest Post by Melt16 on January 14, 2009.
(0)
Melt16
Member
54 points
62 Posts
01-13-2009 10:31 AM |
I have tried using SetValue but I get a 'catastrophic error' when my page loads.Here is my code... RowDefinition row1 = new RowDefinition();row1.SetValue(Grid.HorizontalAlignmentProperty, HorizontalAlignment.Center);grid.RowDefinitions.Add(row1);What is wrong? Thanks
RowDefinition
prujohn
Contributor
3567 points
703 Posts
01-13-2009 10:49 AM |
A row definition does not support the HorizontalAlignment property.
Apply this property to elements you place within your grid.
Also, HorizontalAlignment is related to a child element's position in a column, not a row. Alignment to a row would be VerticalAlignment.
Here's an illustrative example in C#, but I'd recommend you do as much of this in Xaml as possible:
Grid g = new Grid(); //create 2 columns in the grid g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30) }); g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30) }); //Define some elements TextBlock t = new TextBlock() { Text = "Aligned left", HorizontalAlignment = HorizontalAlignment.Left }; TextBlock t2 = new TextBlock() { Text = "Aligned center", HorizontalAlignment = HorizontalAlignment.Center }; //Assign grid positions Grid.SetColumn(t, 0); Grid.SetColumn(t2, 1); //Add the elements to the grid's collection g.Children.Add(t); g.Children.Add(t2);
01-14-2009 2:18 AM |
Why would you recommend doing this in XAML over C#?I would be using C# for this as I am dynamically adding content to my page based on user interaction.