我总是在 windows phone 中定位元素时遇到问题。希望有人可以帮助我:我有一个从后面的代码动态填充的列表框:
<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>
并且看起来工作正常,除了对齐属性。
如果列表框项目的长度相同,则一切正常,但如果其中一个较长,则其他所有项目都将自己对齐到较长项目的开头而不是保持居中:
我该如何解决这个问题?