我有一个组合框,VerticalContentAlignment
设置为Center
,但它似乎仍然不是中心。我意识到问题在于它以小写字母为中心,而不是整个文本。因此,如果您有This Is An Example
之类的文字并测量小写e
的顶部/底部空格,那么它就会居中。但是,大写字母使它看起来更加顶对齐。在许多应用程序中,这是处理....有没有办法在WPF中处理这个?
答案 0 :(得分:1)
您可以使用ComboBox.ItemTemplate
property来定义ComboBoxItem
应该是什么样子。然后,您可以根据自己的需要安排项目内容:
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
如果这不符合您的要求,您也可以使用Run.BaselineAlignment
property获取更多选项:
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center">
<Run BaselineAlignment="TextBottom" Text="{Binding}" />
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我只是想......您可以使用TextBlock.Margin
或TextBlock.Padding
属性将文字放在您想要的位置:
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="0,2,0,0" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>