Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

truncate - Can you limit the length of Text visible in a WPF TextBlock?

I have a listbox that is bound to a database query result. I'm using an item template that shows the subject on one line and I want it to show preview of the body on another line. What I'm wondering is that-- obviously the body is going to be way too long to fit in there, can I somehow set it to only display the first so many characters and append an ellipses after that, like a preview? Or even something close would be fine. So for example:

Instead of displaying:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vitae eros nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec augue metus, iaculis id porta non, pellentesque quis turpis. Donec rutrum diam eget tortor bibendum vel blandit odio iaculis. Curabitur pretium adipiscing orci, ut pulvinar justo vehicula non. Mauris nec ipsum velit. Sed et auctor nibh. Proin ac ultricies tellus.

It would display something like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse...

any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use 'TextTrimming' Property of a Textblock. Set TextTrimming = "CharacterEllipsis". You might need to play with Width to manage how many characters you really want to display.

<TextBlock TextTrimming="CharacterEllipsis" Text="This is a sample long text. This will get Trimmed."/>

Add this textblock in your item template


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...