wpf 带有省略号和冒号的文本修剪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9630548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextTrimming with Ellipsis and a Colon
提问by FZdev
This is a relatively simple question:
这是一个比较简单的问题:
I can trim a text with ellipsis using this:
我可以使用以下方法修剪带有省略号的文本:
<TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
it would give me something along the lines of:
它会给我一些类似的东西:
"This sentence is too long"
“这句话太长了”
=>
=>
"This sentence i..."
“这句话我……”
That's all great and dandy, but what I actually want is this:
这一切都很棒,但我真正想要的是:
"This sentence ...:" / "This sentence...:"
“这句话……:”/“这句话……:”
What I'm looking for is a colon after the ellipses. Is there a simple way to achieve this?
我正在寻找的是省略号后的冒号。有没有简单的方法来实现这一目标?
EDIT:
编辑:
sorry for the confusion.
对困惑感到抱歉。
I want to change the default ellipsis string from '...' to '...:'. As well, I'm going to include a colon in the text string itself. This way, I'll always have the colon displayed. As well, everything should be on one line in every situation.
我想将默认省略号字符串从“...”更改为“...:”。同样,我将在文本字符串本身中包含一个冒号。这样,我将始终显示冒号。同样,在每种情况下,一切都应该在一条线上。
here are a couple of results that are acceptable:
这里有几个可以接受的结果:
short enough:
足够短:
way too l...:
我也...:
回答by Phil
This works, but I needed to add some padding so that the colon always remains visible:
这有效,但我需要添加一些填充,以便冒号始终可见:
<TextBlock Padding="0,0,5,0" >
<TextBlock TextTrimming="CharacterEllipsis">Lorem ipsum dolor sit amet, consectetur adipisicing </TextBlock>
<TextBlock>:</TextBlock>
</TextBlock>
回答by Doug Ferguson
Use two TextBlocks with the ellipses example in the first and the colon in the second.
使用两个 TextBlocks,第一个例子是省略号,第二个例子是冒号。
Update:
更新:
It looks like this is a relatively simple question with plenty of complications.
看起来这是一个相对简单的问题,但有很多复杂性。
One may be tempted to have some TextBlocks, the first with the target text and another two displaying ":" and "...:" and switch between with a Visibility value converter them based on whether the first TextBlock had enough space to display all of its text. This has possibilities but has the potential for unstable layouts.
人们可能会想要一些 TextBlocks,第一个带有目标文本,另外两个显示“:”和“...:”,并根据第一个 TextBlock 是否有足够的空间来显示所有内容,并使用 Visibility 值转换器在它们之间切换其文本。这有可能,但有可能导致布局不稳定。
Having just implemented a custom panel I can imagine a possible solution involving one designed to hold three children which would be the three TextBlocks described abovel
刚刚实现了一个自定义面板,我可以想象一个可能的解决方案,其中涉及一个旨在容纳三个孩子的解决方案,这将是上述三个 TextBlocksl
A custom panel inherited from Panel should override two key methods: Measure and Arrange.
从 Panel 继承的自定义面板应覆盖两个关键方法:测量和排列。
In the measure method all of the children should be measured.
在测量方法中,所有的孩子都应该被测量。
In the arrange method check whether there is enough room to display the first two children and if so put them side by side. If there is not enough room display the first child sized to allow the third child room to display and set the third child right aligned.
在排列方法中检查是否有足够的空间来显示前两个孩子,如果有,将它们并排放置。如果没有足够的空间显示第一个孩子的大小以允许第三个孩子房间显示并设置第三个孩子右对齐。
Update:
更新:
I tried the custom panel and it worked except the the first TextBox is clips partial characters.
我尝试了自定义面板,它工作正常,除了第一个 TextBox 是剪辑部分字符。
The ultimate solution for clean formatting would be a method which adjust the display string until fits within the allotted space with the appropriate suffix applied.
干净格式化的最终解决方案是调整显示字符串直到适合分配的空间并应用适当的后缀的方法。