如何使 ComboBox 的内容垂直居中?
例如,请注意文本并不完全位于组合框的垂直中心。
这是我的 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须使用它,但如果我不得不猜测:
尝试将
MinHeight="30"
更改为较小的数字。可能是您将框设置得比文本大。文本在线居中,但框更大。You have to play with it, but if I had to guess:
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:
So when i copied and pasted your code in, it worked for me!
您可以更改垂直/水平对齐方式,如下所示:
you can change the vertical/horizontal alignment as so:
将
VerticalContentAlignment="Center"
添加到组合框。Add
VerticalContentAlignment="Center"
to your combobox.