2013-10-09 55 views
1

我总是遇到在windows phone中定位元素的问题。希望有人能帮助我: 我有一个列表框dinamically从代码populatem背后:在ItemTemplate中定位文本 - Windows Phone 8

<ListBox Name="list" Grid.Row="1" HorizontalAlignment="Center"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" Style="{StaticResource list_service_item}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 

风格在App.xaml中定义:

<Style x:Key="list_service_item" TargetType="TextBlock"> 
     <Setter Property="FontSize" Value="25"/> 
     <Setter Property="FontWeight" Value="Bold"/> 
     <Setter Property="Foreground" Value="Peru" /> 
     <Setter Property="TextWrapping" Value="Wrap"/> 
     <Setter Property="HorizontalAlignment" Value="Center"/> 
     <Setter Property="Margin" Value="0 0 0 5"/> 
    </Style> 

貌似除了正常工作对齐属性。

如果列表框项具有相同的长度一切运作良好,但是,如果他们中的一个较长所有其他赞同的长项的开始,而不是保持居中:

enter image description here

如何我能解决这个问题吗?

+0

你肯定* *,他们正在调整到较长的项目开始而不仅仅是集中,而是巧合地与长项目的开始排队?你能向我们展示一张图片吗? – Sheridan

+0

这是一个[截图](http://i338.photobucket.com/albums/n430/jack_the_beast/items.png) –

回答

1

你需要做ItemContainer每个ListBoxItem的拉伸到列表框的宽度:

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

谢谢你的工作 –

0

如何使用TextBlock.TextAlignment财产?:

<Style x:Key="list_service_item" TargetType="TextBlock"> 
    <Setter Property="FontSize" Value="25" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="Foreground" Value="Peru" /> 
    <Setter Property="TextWrapping" Value="Wrap" /> 
    <Setter Property="HorizontalAlignment" Value="Center" /> 
    <Setter Property="TextBlock.TextAlignment" Value="Center" /><!--<<< Used here--> 
    <Setter Property="Margin" Value="0 0 0 5"/> 
</Style> 

免责声明:本工程为WPF,但我不能保证它适用于Windows Phone 8的

UPDATE >> >

好的,看到你的照片后,我会同意它不能按照你的预期工作。但是,我认为这可能是更多的情况下,项目没有正确放置。你可以试试这个:

<ListBox HorizontalContentAlignment="Center" ... /> 

如果不工作,你可以试试属性设置为Stretch,这样的项目填补了空间,那么可以给TextBlock S中居中自己:

<ListBox HorizontalContentAlignment="Stretch" ... /> 
+0

不行......它不起作用... –

+0

这个解决方案似乎对齐所有项目到左边......我不知道为什么......但是我用Doc的答案解决了。非常感谢你:) –