Как я могу центрировать содержимое ComboBox по вертикали? - PullRequest
23 голосов
/ 15 мая 2011

Например, обратите внимание, что текст не совсем в вертикальном центре 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 ]

50 голосов
/ 15 мая 2011

Добавьте VerticalContentAlignment="Center" к вашему списку.

4 голосов
/ 15 мая 2011

Если я скопирую и вставлю ваш код, для меня текст будет выровнен по вертикали в центре ComboBox. Вы уверены, что в вашем приложении не настроен стиль или шаблон, который применяется к вашим элементам управления и делает это возможным?

РЕДАКТИРОВАТЬ: Неважно. На самом деле в моем приложении настроен стиль:

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

Так что, когда я скопировал и вставил ваш код, он работал для меня!

4 голосов
/ 15 мая 2011

Вы должны поиграть с ним, но если бы мне пришлось угадывать:

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

Попробуйте изменить MinHeight="30" на меньшее число.Возможно, вы делаете окно больше, чем текст.Текст центрируется на строке, но поле больше.

0 голосов
/ 15 октября 2018

Вы можете изменить вертикальное / горизонтальное выравнивание следующим образом:

<ComboBox x:Name="cmbPlatform" Margin="10" FontFamily="Georgia" FontSize="15"
                  MinHeight="30" MinWidth="140"
                  VerticalContentAlignment="Center" 
                  HorizontalContentAlignment="Center">
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...