mskuratowski
mskuratowski

Reputation: 4124

Align control on the right in grid

I have a grid with 3 columns:

<Grid.ColumnDefinitions>
 <ColumnDefinition Width="Auto" />
 <ColumnDefinition Width="*" />
 <ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

I want to have the last column align on the right, near to edge of screen. Currently, the third column occurs soon after the second. I don't want to pass width in pixels, rather automatic calculation.

I have tried to use on the element in third column property HorizontalAlignment="Right", but unfortunately it's not working.

Upvotes: 1

Views: 1151

Answers (3)

Burak Kaan K&#246;se
Burak Kaan K&#246;se

Reputation: 831

If your Grid is inside of a Stackpanel , no matter what you do it'll only resize iteself to the width of Stackpanel.

If your last column definitions is Auto , you can't align it to the right of the screen.Change 2nd definition to Auto and the last one to *.

Upvotes: 1

nvi9
nvi9

Reputation: 1893

If you have the same type of controls inside the grid, you can assign a style for all of them in <Grid.Resources>, with setting the HorizontalAlignment property only if the Grid.Column property's value is 2 (with triggers). But it is even easier for looping through all the children, and checking the column property of each and then setting the alignment, like

foreach (Control ctrl in yourGrid.Children) {
    if(Grid.GetColumn(ctrl) == 2) ctrl.HorizontalAlignment = HorizontalAlignment.Right;
}

Upvotes: 0

Abhishek
Abhishek

Reputation: 1399

Since your last column has Auto width, setting HorizontalAlignement of the content to Right should not be necessary. My guess would be either

  1. your Grid doesn't have a HorizontalAlignement value of Stretch (which should be default), or
  2. whatever contains the Grid (a StackPanel maybe?) is not giving it the full width.

I could be sure if I could check the rest of the XAML. Check these two things first. If these don't help, please post the rest of your XAML.

Upvotes: 0

Related Questions