如何使ComboBox的内容垂直居中?

34
例如,注意到文本并没有完全处于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个回答

67
在您的组合框中添加 VerticalContentAlignment="Center"

我想要将组合框的列表项水平居中显示。 - Meer

6

你需要和它玩耍,但如果我猜的话:

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

尝试将MinHeight="30"更改为较小的数字。可能是因为你使方框比文本更大。文本在行上居中,但方框更大。

好的,下拉框已经垂直居中了,我同意。但是我也想将下拉框内的内容居中对齐。 - Only Bolivian Here
将ComboBoxItem的垂直对齐方式设置为居中也没有任何效果。 - Only Bolivian Here
1
@Sergio 垂直对齐方式 - Hogan

5
如果我复制并粘贴你的代码,文本会在ComboBox的垂直中心对齐。您确定您的应用程序中没有设置样式或模板,以应用于控件并导致出现此情况吗?
编辑:不要紧。实际上,我在我的应用程序中设置了一个样式:
<Style TargetType="{x:Type ComboBox}">
        <Setter Property="VerticalContentAlignment" Value="Center" />
</Style>

所以当我复制粘贴您的代码时,它对我起作用了!


3

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

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

2
欢迎来到StackOverflow!虽然这段代码可能可以回答问题,但最好加上一些背景信息,解释它的工作原理和使用场景。这将使您的答案在长期内更有用。 - stop-cran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接