【问题标题】:Change TextBlock Text in listBox更改列表框中的 TextBlock 文本
【发布时间】:2012-06-23 19:35:36
【问题描述】:

我有一个ListBox,它由以下项目组成:

<ListBoxItem>
    <ListBoxItem.ContentTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=department}"></TextBlock>
        </DataTemplate>
    </ListBoxItem.ContentTemplate>
</ListBoxItem>

部门是一个字符串属性:text="dept1\dept2\dept3\dept4\dept5\dept6" 我的问题是,当ListBox 大小发生变化时,我想以这种方式更改TextBlock 文本:text=dept1\dept2\...\dept6。

(dept1\dept2\...\dept6) 的长度等于或小于ListBoxItem 实际大小。

【问题讨论】:

    标签: wpf xaml text textblock listboxitem


    【解决方案1】:

    您需要像这样将 TextBlock 的 TextTrimming 属性设置为 CharacterEllipsis -

    <TextBlock Text="{Binding Path=department}" TextTrimming="CharacterEllipsis"/>
    

    但是,如果它大于列表框的大小,则会在文本末尾显示省略号。像这样的东西 - “dept1\dept2\dept3\dept4\dept5...”

    但是,如果您希望它位于中心,您可能必须使用ValueConverter 来格式化字符串。这可能会有所帮助 - Ellipsis at start of string in WPF ListView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多