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

发布于 2024-11-07 22:37:36 字数 2144 浏览 0 评论 0原文

例如,请注意文本并不完全位于组合框的垂直中心。

在此处输入图像描述

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

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

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光匆匆的小流年 2024-11-14 22:37:37

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

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

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

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.

提笔书几行 2024-11-14 22:37:37

如果我复制并粘贴您的代码,文本将在组合框的中心垂直对齐。您确定您的应用程序中没有设置应用于您的控件并实现此目的的样式或模板吗?

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

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

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

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!

七婞 2024-11-14 22:37:37

您可以更改垂直/水平对齐方式,如下所示:

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

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">
飘过的浮云 2024-11-14 22:37:36

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

Add VerticalContentAlignment="Center" to your combobox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文