TheUnexpected
TheUnexpected

Reputation: 3167

Align Grid column to right

I have a Windows Phone / XAML Grid composed by 3 columns. In particular, I want the third column to be aligned to the very right side of the screen.

<Grid Background="Transparent" Margin="0,3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

    <Image Grid.Column="0" x:Name="Marker" Width="60" Height="60" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/>
    <TextBlock Grid.Column="1" x:Name="Name" TextAlignment="Left" VerticalAlignment="Center" Margin="20,0" />
    <Image Grid.Column="2" x:Name="Selected" Width="48" Height="48"  VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/>
 </Grid>

The result, instead, is this:

enter image description here

When it should be like this:

enter image description here

Upvotes: 26

Views: 48055

Answers (4)

Swapnil Karmalkar
Swapnil Karmalkar

Reputation: 21

I had same problem and it was fixed after removing the stackpanel

Upvotes: 0

Rohit Vats
Rohit Vats

Reputation: 81233

As you mentioned its an ItemTemplate of ListBox, what you can do is set HorizontalContentAlignment to Stretch.

    <ListBox>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

Upvotes: 31

mrzli
mrzli

Reputation: 17359

Try HorizontalAlignment="Stretch" in Grid.

Upvotes: 0

har07
har07

Reputation: 89285

Try with this :

<Grid Background="Transparent" Margin="0,3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    <StackPanel Orientation="Horizontal">
        <Image x:Name="Selected" Width="48" Height="48"  VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/>
        <TextBlock x:Name="Name" TextAlignment="Left" VerticalAlignment="Center" Margin="20,0" />
    </StackPanel>
    <Image Grid.Column="1" x:Name="Selected" Width="48" Height="48"  VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Right"/>
 </Grid>

Upvotes: 2

Related Questions