简体   繁体   English

正确对齐Button / StackPanel中的TextBlock

[英]Align TextBlock in Button / StackPanel correctly

I try to align the TextBlocks to the left. 我尝试将TextBlocks对齐到左侧。 The button should take the rest of the area (Up to the picture). 该按钮应占据其余区域(上图)。 How can I do this? 我怎样才能做到这一点?

This is a UWP / C# App. 这是一个UWP / C#应用程序。

在此处输入图片说明

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Left">

        <Button Background="Transparent" HorizontalAlignment="Stretch">
            <StackPanel HorizontalAlignment="Left">
                <TextBlock x:Name="TextBlock1" TextWrapping="Wrap" Text="Name1"/>
                <TextBlock x:Name="TextBlock2" TextWrapping="Wrap" Text="Name2" />
            </StackPanel>
        </Button>

    </Grid>

    <StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Right">
        <Button Height="50" Width="50" Background="Transparent" Click="Button_Click">
            <Image x:Name="ImageFavorit" Source="/Assets/Stern/VGrau64.png"/>
        </Button>
    </StackPanel>
</Grid>

Firstly you should replace your ColumnDefinitions to this: 首先,您应该将ColumnDefinitions替换为:

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

And secondly remove HorizontalAlignment="Left" from your Grid then you can achieve to what you want: 然后从Grid删除HorizontalAlignment="Left" ,然后可以实现所需的功能:

<Grid VerticalAlignment="Center">
    <Button Background="Transparent" HorizontalAlignment="Stretch">
    ...

Also it seems that you need to set VerticalAlignment="Center" of your StackPanel too: 同样,您似乎也需要设置StackPanel VerticalAlignment="Center"

<StackPanel Grid.Column="1" Grid.Row="0"
            HorizontalAlignment="Right" VerticalAlignment="Center">

Update: Based on your comment that you want the text on the left side Set the HorizontalContentAlignment="Left" of the Button : 更新:根据您的评论,您希望左侧的文本设置ButtonHorizontalContentAlignment="Left"

<Button Background="Transparent" HorizontalAlignment="Stretch" 
        HorizontalContentAlignment="Left">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM