wpf 如何垂直居中 ComboBox 的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6004792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I center a ComboBox's content vertically?
提问by Only Bolivian Here
For example, notice how the text isn't quite in the vertical center of the ComboBox.
例如,请注意文本如何不完全位于 ComboBox 的垂直中心。
Here's my XAML:
这是我的 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>
回答by Alex Aza
Add VerticalContentAlignment="Center"
to your combobox.
添加VerticalContentAlignment="Center"
到您的组合框。
回答by Ross
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?
如果我复制并粘贴您的代码,文本将垂直对齐在 ComboBox 的中心。您确定您的应用程序中没有设置样式或模板来应用到您的控件并实现这一点吗?
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!
因此,当我复制并粘贴您的代码时,它对我有用!
回答by Hogan
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.
尝试将 更改MinHeight="30"
为较小的数字。可能是您使框比文本大。文本以行为中心,但框更大。
回答by AHM
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">