2011-05-14 23 views
19

Por ejemplo, observe cómo el texto no está exactamente en el centro vertical del ComboBox.¿Cómo puedo centrar el contenido de un ComboBox verticalmente?

enter image description here

Aquí es mi 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> 

Soy una especie de nuevo en WPF, y yo nunca he hecho nada de principio a fin con ella. También agradecería cualquier consejo sobre cómo mejorar algunos errores novatos flagrantes que pueda detectar.

Respuesta

40

Agregue VerticalContentAlignment="Center" a su cuadro combinado.

+0

Quiero mostrar los elementos de la lista de combobox horizontalmente centro – Meer

2

Hay que jugar con él, pero si tuviera que adivinar:

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

Intente cambiar el MinHeight="30" a un número más pequeño. Puede ser que estés haciendo la caja más grande que el texto. El texto está centrado en la línea, pero el cuadro es más grande.

+0

Ok, el cuadro combinado se centra verticalmente, estoy de acuerdo. Pero también quiero centrar el contenido dentro del cuadro combinado. –

+0

Establecer la alineación vertical al centro de un ComboBoxItem no hace nada tan bien. –

+0

@Sergio VerticalContentAlignment – Hogan

3

Si copio y pego su código, el texto se alinea verticalmente en el centro de ComboBox para mí. ¿Estás seguro de que no tienes un Estilo o una Plantilla configurados en tu aplicación que se aplica a tus controles y hace que esto suceda?

EDIT: Nevermind. En realidad tenía un estilo creado en mi solicitud:

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

Así que cuando copiar y pegar el código en, que trabajó para mí!

Cuestiones relacionadas