Align Grid column to right

41,173

Solution 1

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>

Solution 2

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>
Share:
41,173

Related videos on Youtube

TheUnexpected
Author by

TheUnexpected

Updated on August 18, 2022

Comments

  • TheUnexpected
    TheUnexpected over 1 year

    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

    • Sam Leach
      Sam Leach over 10 years
      Is there no HorizontalAlignment="Right" ?
    • TheUnexpected
      TheUnexpected over 10 years
      Yes, and it doesn't work :(
    • Rohit Vats
      Rohit Vats over 10 years
      Is this Grid a part of ItemTemplate of ListBox because i can't see other items in Grid.
    • TheUnexpected
      TheUnexpected over 10 years
      Yes, it's a user control created to customize each item on a ListBox
    • Rohit Vats
      Rohit Vats over 10 years
      Set HorizontalContentAlignment to Stretch on ListBoxItem. Check my answer.
  • RudolfJan
    RudolfJan about 6 years
    Using a ListView instead of a ListBox may make life earier for multi column lists.
  • RudolfJan
    RudolfJan about 5 years
    This is standard stuff. Look up a tutorial on ListViews, e.g. blackwasp.co.uk/WPFListView.aspx gives you a nice example. I always use ListView or DataGrid for tables.