2012-06-23 43 views
0

更改TextBlock的文字我有一个ListBox其中包括从像这样的项目:在列表框中

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

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

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

回答

0

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

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

但是,这将在文本的末尾显示省略号如果它得到比ListBoxItem的规模更大。像这样的东西 - “dept1 \ dept2 \ dept3 \ dept4 \ dept5 ...”

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