开发者

How can I center a ComboBox's content vertically?

For example, notice how the text isn't quite in the vertical center of the ComboBox.

How can I center a ComboBox's content vertically?

Here's my 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="3开发者_如何学Python50">


    <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>


Add VerticalContentAlignment="Center" to your combobox.


You have to play with it, but if I had to guess:

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

Try changing the MinHeight="30" to a smaller number. It might be you are making the box bigger than the text. The text is centered on the line but the box is bigger.


If I copy and paste your code, the text is vertically aligned in the center of the ComboBox for me. Are you sure you don't have a Style or Template set up in your application that is applying to your controls and making this happen?

EDIT: Nevermind. I actually had a style set up in my application:

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

So when i copied and pasted your code in, it worked for me!


you can change the vertical/horizontal alignment as so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜