Skip to main content

Microsoft Silverlight

Answered Question How can I set the horizontalalignment of an individual row in grid?RSS Feed

(0)

Melt16
Melt16

Member

Member

54 points

62 Posts

How can I set the horizontalalignment of an individual row in grid?

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

prujohn
prujohn

Contributor

Contributor

3567 points

703 Posts

Answered Question

Re: How can I set the horizontalalignment of an individual row in grid?

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);
 

Melt16
Melt16

Member

Member

54 points

62 Posts

Re: Re: How can I set the horizontalalignment of an individual row in grid?

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities