2012-11-08 235 views
0

我有一個列表框 - 列表框.ItemTemplate - DataTemplate xaml安裝程序,我試圖將文本居中列表框(TextBlock)中。 TextAlignment屬性似乎不起作用。有任何想法嗎?這裏是我的代碼:Windows 8 ListBox TextAlignment不工作

<ListBox x:Name="listBox1" HorizontalAlignment="Left" Height="520" Margin="190,220,0,0" VerticalAlignment="Top" Width="295" SelectionChanged="listBox1_SelectionChanged" FontSize="20" FontWeight="Bold" > 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding name}" FontSize="25" Foreground="Black" TextAlignment="Center"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

回答

1

列表框使用一個StackPanel內部包含每個ItemTemplate中默認情況下,ItemTemplate中的大小隻有大到足以容納它的孩子。在您的模板中,文本塊只會自行調整大小以適應綁定文本,因此不存在「中心」,因爲邊界文本塊與文本的大小完全匹配。明確地設置你的文本塊的大小,或者你可以添加以下內容到你的列表框中(或者在你的頁面中聲明爲可重複使用的風格)

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

謝謝傑夫,它修復了它。 methinks。整個過程有點複雜。 –

相關問題