wpf 将网格列向右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20934261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:02:33  来源:igfitidea点击:

Align Grid column to right

wpfxamlwindows-phone-7grid

提问by TheUnexpected

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.

我有一个由 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>

The result, instead, is this:

结果是这样的:

enter image description here

在此处输入图片说明

When it should be like this:

什么时候应该是这样的:

enter image description here

在此处输入图片说明

回答by Rohit Vats

As you mentioned its an ItemTemplateof ListBox, what you can do is set HorizontalContentAlignmentto Stretch.

正如您提到ItemTemplate的 ListBox 的一个,您可以做的是设置HorizontalContentAlignmentStretch.

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

回答by har07

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>

回答by mrzli

Try HorizontalAlignment="Stretch"in Grid.

HorizontalAlignment="Stretch"在网格中尝试。

回答by Swapnil Karmalkar

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

我遇到了同样的问题,在移除堆栈面板后已修复