【问题标题】:How can I center a ComboBox's content vertically?如何将 ComboBox 的内容垂直居中?
【发布时间】:2011-05-14 21:09:01
【问题描述】:

例如,请注意文本如何不在 ComboBox 的垂直中心。

这是我的 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 xaml combobox


    【解决方案1】:

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

    【讨论】:

    • 我想水平居中显示组合框的列表项
    【解决方案2】:

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

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

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

    【讨论】:

    • 好的,组合框垂直居中,我同意。但我也想将组合框中的内容居中。
    • 将垂直对齐设置为 ComboBoxItem 的中心也无济于事。
    • @Sergio VerticalContentAlignment
    【解决方案3】:

    如果我复制并粘贴您的代码,文本会在 ComboBox 的中心垂直对齐。您确定您的应用程序中没有设置样式或模板来应用到您的控件并实现这一点吗?

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

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

    所以当我复制并粘贴你的代码时,它对我有用!

    【讨论】:

      【解决方案4】:

      你可以改变垂直/水平对齐方式:

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

      【讨论】:

      • 欢迎来到 StackOverflow!虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,这将使您的答案更有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2016-09-10
      • 2017-08-16
      • 2015-03-01
      • 2017-12-13
      相关资源
      最近更新 更多