GridLength.Star Not Working For HorizontalAlignment="Center"

Nathan Sokalski 4,116 Reputation points
2020-05-07T17:29:26.647+00:00

I have a Grid as follows:

<Grid Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions><RowDefinition Height=""/><RowDefinition Height=""/></Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="
"/>
</Grid.ColumnDefinitions>
<Border Padding="5" BorderBrush="Black" BorderThickness="1,1,0.5,1"><Path HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="0" Fill="Green" Data="M10 20H0L19.5 0L40 20H29V26.5H10Z"/></Border>
<Border Grid.Column="1" Padding="5" BorderBrush="Black" BorderThickness="0.5,1,0.5,0.5"><TextBlock FontFamily="Arial" FontSize="32" Foreground="Black" TextAlignment="Center" OpticalMarginAlignment="TrimSideBearings" TextLineBounds="Tight" Text="2" VerticalAlignment="Center"/></Border>
</Grid>

Note that all ColumnDefinition(s) have Width="*", which I would expect to make all columns the same width. However, this is giving me the following:

7927-gridcenter.png

But if I change the Grid to HorizontalAlignment="Stretch" (instead of Center), I get the following:
7840-gridstretch.png

Is there any way to center the Grid without wrapping it in another element (such as another Grid)? Thanks.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniele 1,996 Reputation points
    2020-05-07T18:35:30.663+00:00

    You defined too many columns, try to put only 2 ColumnDefinitions