2011-05-14 1146 views
19

例如,請注意文本如何不在ComboBox的垂直中心。如何垂直居中ComboBox的內容?

enter image description here

這裏是我的XAML:

<Window x:Class="_24HoursBook.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="450" Width="350" MinHeight="450" MinWidth="350"> 


    <Grid ShowGridLines="True"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="0.15*" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <Image Grid.Row="0" Stretch="Fill" Source="Image/topBarBg.png" /> 
     <StackPanel Orientation="Horizontal" Grid.Row="0">    
      <TextBlock Text="Platform" 
         Foreground="White" 
         FontFamily="Georgia" 
         FontSize="15" 
         Margin="10" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center"/> 
      <ComboBox x:Name="cmbPlatform" 
         Margin="10" 
         FontFamily="Georgia" 
         FontSize="15" 
         MinHeight="30" 
         MinWidth="140" 
         VerticalAlignment="Center"> 
       <ComboBoxItem>All Platforms</ComboBoxItem> 
       <ComboBoxItem>Playstation 3</ComboBoxItem> 
       <ComboBoxItem>XBox 360</ComboBoxItem> 
       <ComboBoxItem>Wii</ComboBoxItem> 
       <ComboBoxItem>PSP</ComboBoxItem> 
       <ComboBoxItem>DS</ComboBoxItem> 
      </ComboBox>    
     </StackPanel> 
     <Image Grid.Row="0" Source="Image/about.png" 
       Height="16" HorizontalAlignment="Right" 
       VerticalAlignment="Center" 
       Margin="0 0 10 0" /> 

     <ListView Grid.Row="1" Background="#343434"> 

     </ListView> 
    </Grid> 
</Window> 

我有點新的WPF,我從來沒有真正做過從開始任何與它完成。我也很感激任何關於改善你可能會遇到的一些明顯的新手錯誤的提示。

回答

40

VerticalContentAlignment="Center"添加到您的組合框中。

+0

我想顯示組合框水平中心的列表項 – Meer 2016-03-30 09:59:24

2

你必須發揮它,但如果要我猜:

<ComboBox x:Name="cmbPlatform" 
        Margin="10" 
        FontFamily="Georgia" 
        FontSize="15" 
        MinHeight="30" 
        MinWidth="140" 
        VerticalAlignment="Center" 
        VerticalContentAlignment="Center"> 

嘗試改變MinHeight="30"一個較小的數字。這可能是你讓這個盒子比文字更大。文字以線爲中心,但框較大。

+0

好吧,組合框是垂直居中,我同意。但我想將內容集中在組合框內。 – 2011-05-14 21:15:11

+0

將垂直對齊設置爲ComboBoxItem的中心也不起作用。 – 2011-05-14 21:16:43

+0

@Sergio VerticalContentAlignment – Hogan 2011-05-14 21:23:24

3

如果我複製並粘貼代碼,文本將在我的組合框的中心垂直對齊。您確定您的應用程序中沒有設置適用於您的控件並將其實現的樣式或模板嗎?

編輯:沒關係。其實我有一個風格在我的應用程序設置:

<Style TargetType="{x:Type ComboBox}"> 
     <Setter Property="VerticalContentAlignment" Value="Center" /> 
</Style> 

所以當我複製並粘貼在你的代碼,它爲我工作!