如何将文本移动到ListBox的中心?

时间:2012-02-03 19:07:11

标签: c# xaml windows-phone

我几乎尝试了所有东西,但由于某种原因它无法正常工作

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
    <TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
    <ListBox HorizontalContentAlignment="Stretch" Background="Red" ItemsSource="{Binding Path=ViewOptions}" Margin="10">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

以上XAML给我

enter image description here

如何将textblock移至center的{​​{1}}?

3 个答案:

答案 0 :(得分:3)

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
    <TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
    <ListBox x:Name="listBox" HorizontalContentAlignment="Stretch"  ItemsSource="{Binding Path=ViewOptions}" Margin="10,30,10,10">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                 <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock  HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
   </ListBox>
</StackPanel>

答案 1 :(得分:1)

您需要将 Horizo​​ntalContentAlignment 更改为居中

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
<TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
<ListBox HorizontalContentAlignment="Center" Background="Red" ItemsSource="{Binding Path=ViewOptions}" Margin="10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

答案 2 :(得分:1)

我刚试过这个并且有效。两种可能的差异:

  • 我的数据来源很简单List<string>
  • 我删除了对您的样式的引用(即PhoneTextNormalStyle)。

您是否绑定了简单数据?

PhoneTextNormalStyle是否指定左对齐?