如何在WPF ComboBox中正确显示太长的文本 [英] How to display too long text properly in WPF ComboBox

查看:748
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆