文本块未正确对齐

时间:2014-11-26 15:27:18

标签: c# xaml windows-phone-8.1

我有以下XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.10*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
    </Grid>
    <StackPanel Grid.Row="0" Background="#FFEC850A" Orientation="Horizontal">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.5*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
        </Grid>
        <Button x:Name="BtnHashtag" Foreground="White" Content="# Tag" BorderBrush="{x:Null}" Grid.Column="0" FontSize="28" FontFamily="Assets/font/vijaya.ttf#Vijaya" FontWeight="Bold"/>
        <TextBlock x:Name="TxtDate" Foreground="White" Text="Date : 23/11/2014" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="28" FontFamily="Assets/font/vijaya.ttf#Vijaya" FontWeight="Bold" Grid.Column="1" TextAlignment="Right"/>
    </StackPanel>
    <TextBox Background="White" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" AcceptsReturn="True"/>
</Grid>

这就是它的样子: enter image description here

我想在stackpanel里面放两个文本。首先是#tag,另一个是date。如何以#tag占据stackpanel的一半宽度并且date占据另一半的方式来定义它。此外,两个文本(#tag和日期)都应出现在其列的中心。即它看起来应该是这样的: enter image description here

另外我想放置一个分隔符,如|两列之间。怎么做?

1 个答案:

答案 0 :(得分:2)

此布局中不需要StackPanel。你可以完全删除它并只保留Grid,平均分割列并将每个元素放在其单元格中

<Grid Grid.Row="0" Background="#FFEC850A">
   <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
   </Grid.ColumnDefinitions>
   <Button Grid.Column="0" x:Name="BtnHashtag" Foreground="White" Content="# Tag" BorderBrush="{x:Null}" Grid.Column="0" FontSize="28" FontFamily="Assets/font/vijaya.ttf#Vijaya" FontWeight="Bold"/>
   <TextBlock Grid.Column="1" x:Name="TxtDate" Foreground="White" Text="Date : 23/11/2014" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="28" FontFamily="Assets/font/vijaya.ttf#Vijaya" FontWeight="Bold" Grid.Column="1" TextAlignment="Right"/>
</Grid>

水平StackPanel会忽略其子项的HorizontalAlignment,并将它们对齐