将网格列对齐

时间:2014-01-05 13:43:31

标签: wpf xaml windows-phone-7 grid

我有一个由3列组成的Windows Phone / XAML网格。特别是,我希望第三列与屏幕的右侧对齐。

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

结果是:

enter image description here

什么时候应该是这样的:

enter image description here

4 个答案:

答案 0 :(得分:21)

正如您提到的ItemTemplate ListBox,您可以将HorizontalContentAlignment设置为Stretch

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

答案 1 :(得分:2)

试试这个:

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

答案 2 :(得分:0)

在网格中尝试HorizontalAlignment="Stretch"

答案 3 :(得分:0)

我遇到了同样的问题,删除了stackpanel后它被修复了