如何在WPF ComboBox中正确显示太长的文本 [英] How to display too long text properly in WPF ComboBox
问题描述
我有一个ComboBox,可以显示各种长度的文本.对于不长的文本,没有问题.对于长度超过ComboBox宽度的文本,我要修剪文本并在末尾添加"..."(省略号)以正确显示它们.最重要的是,我不想更改ComboBox的宽度.有人知道该怎么做吗?
为您的ComboBox
使用自定义ItemTemplate
,该自定义ItemTemplate
使用TextBlock
,并且TextTrimming
属性设置为CharacterEllipsis
./p>
示例:
<ComboBox ItemsSource="..." SelectedValuePath="...">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ...}"
TextTrimming="CharacterEllipsis" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I have a ComboBox that shows text of various lengths. For texts that are not long there is not a problem. For the texts longer than the width of ComboBox I would like to trim the text and add "..." (an ellipsis) at the end to show them properly. The bottom line is that I don't want to change the width of the ComboBox. Does anyone know how to do this?
Use a custom ItemTemplate
for your ComboBox
, which makes use of a TextBlock
with the TextTrimming
property set to CharacterEllipsis
.
Example:
<ComboBox ItemsSource="..." SelectedValuePath="...">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ...}"
TextTrimming="CharacterEllipsis" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
这篇关于如何在WPF ComboBox中正确显示太长的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!