如何垂直居中ComboBox的内容?

时间:2011-05-14 21:09:01

标签: wpf xaml 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>

4 个答案:

答案 0 :(得分:50)

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

答案 1 :(得分:4)

你必须玩它,但如果我不得不猜测:

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

尝试将MinHeight="30"更改为较小的数字。可能是你让盒子比文字大。文本以行为中心,但框更大。

答案 2 :(得分:4)

如果我复制并粘贴您的代码,则文本将在ComboBox的中心垂直对齐。您确定在应用程序中没有设置适用于控件的样式或模板并实现此目的吗?

编辑:没关系。我实际上在我的应用程序中设置了一个样式:

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

因此,当我复制并粘贴您的代码时,它对我有用!

答案 3 :(得分:0)

您可以这样更改垂直/水平对齐方式:

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