TextTrimming with Ellipsis and a Colon - wpf

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...:

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>

Use two TextBlocks with the ellipses example in the first and the colon in the second.
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.
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
A custom panel inherited from Panel should override two key methods: Measure and Arrange.
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.
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.

If you do not include the colon in your strings, you can use Binding.StringFormat:
<TextBlock Text="{Binding, StringFormat={}{0}:}" TextTrimming="CharacterEllipsis"/>
(I realize this is a very old question, but I happened to stumble on it, so I thought I'd throw this in for anyone else who follows.)

Related

Is it possible to duplicate a shape via a transform (or something else)

I'm trying to create a pause style button in XAML. To create the standard two side-by-side bars, it seems like I should be able to draw a single rectangle, and the apply some sort of transformation to duplicate it and offset the duplicated rectangle, but I've no idea where to begin. Creating two rectangles doesn't seem like the right approach to me, but I don't really know enough about XAML to know for sure
Simple solution is to use the pause symbol from the Webdings font, no elements, transforms required
Example:
<Button FontFamily="Webdings" FontSize="20" Content=";" />

WPF RichTextBox automatically merges two adjacent Runs

I have a RichTextBox containing two Runs:
<RichTextBox>
<RichTextBox.Document>
<FlowDocument>
<FlowDocument.Blocks>
<Paragraph Name="par">
<Run Text="First"/>
<Run Text="Second"/>
</Paragraph>
</FlowDocument.Blocks>
</FlowDocument>
</RichTextBox.Document>
</RichTextBox>
When I iterate through the Inlines of the par, I see three Runs: "First", " "(space) and "Second". It's ok. But if I delete second " " Run (using Backspace key, for example) and then iterate through the Inlines of the par, I see only one "FirstSecond" Run. RichTextBox merges two remaining Runs into single Run. However, if "First" and "Second" Runs have different TextFont or TextWeight values, RichTextBox won't merge them and I'll see two distinct Runs: "First" and "Second".
Now my question: How can I preserve adjacent Runs from being merged automatically by RichTextBox?
I would like to get the behaviour similar to the one when Runs have different TextFont or TextWeight values, but visually their formats should be equal. I've tried to set different Tag values for the different Runs, but it didn't help. Maybe, there is some "logical" format, that doesn't influence the appearance of the Runs, but warns RichTextBox to distinguish them.
Though I haven't found a clear solution, there is a workaround. Run has the Typography property, which, in its turn, has the Int32 AnnotationAlternates property. This property defines the fine tuning of the outlook for the specific characters of the specific fonts. If this property isn't used as intended, it can be used as a "logical" format for a Run. As written in MSDN:
If the value of AnnotationAlternates is greater than zero and the
selected font does not support annotation alternates, the default form
of the letter is displayed.
In my application I use Segoe UI font, which, as it turns out, doesn't support annotation alternates, so this workaround works for me. If you use font that supports annotation alternates, you can try to use rather big values of the AnnotationAlternates property, maybe, it won't influence the appearance of the text.

How to ellipsise in the selected value in a combobox?

In our Silverlight page, I got comboboxes with different values. When selected, some values are too long to fit in the collapsed combobox.
I would like it to ellipsise ("System Use..." with three dots in the end).
In the dropdown the full name of the value can be seen, but when collapsed I would like the selected value to ellipsise. It feels like an obvious thing, as a drop down generally affords more space than the collapsed control, but still I cannot find an obvious way to do this.
You might want to take a look at http://www.codeproject.com/Articles/37503/Auto-Ellipsis.
It's similar to Zenexar's suggestion, uses a more efficient reduction algorithm in the general case. But it also writes about other types of 'ellipsing', which might also suit your needs perhaps.
Take the selected UIElement. Compare the RenderedWidth and RenderedHeight to the DesiredSize, the latter of which will be adjusted based on how the element is cut off. Repeatedly remove characters, append ellipsis, and invalidate the layout until the DesiredSize is smaller than or equal to the RenderedWidth/RenderedHeight
I think the best way would be to check the length of the selected item. If the length is more than a predetermined character length, cut off the rest and append three periods.

Soft hyphens in XAML?

does anybody have an idea whether it is possible to define "soft hyphens" or "soft linebreaks" in e.g. a TextBlock's text? Background: I would like to use TextWrapping="Wrap" on a TextBlock, but normally that won't do anything if the text contained in the TextBlock does not contain white space.
E.g.
<TextBlock TextWrapping="Wrap" Text="OneVeryLongWordThatDoesNotContainAnyWhiteSpaceAtAll" />
won't wrap if there is insufficient space. So I thought maybe there is a way to tell TextWrapping where the text may be wrapped.
I tried using the HTML ­ (soft hyphen) entity, but this is not allowed in XAML apparently (won't compile).
Cheers,
Alex
Alex,
what do you mean by "won't wrap if there is insufficient space"? I tried your example code and it actually does wrap (it will break on every single character if necessary) when adding Width="100" or limiting by it's margins etc.
Setting width to 100 i get the following result:
OneVeryLongWor
dThatDoesNotCon
tainAnyWhiteSpa
ceAtAll
When you limit the height (like Height="20"), it won't actually break of course, but you can add TextTrimming="WordEllipsis" to get a result like this:
OneVeryLongW...
But oh well, it doesn't answer your question about Soft Hyphens, they obviously don't work.
Best regards =)

GtkTextView can't wrap line

Hello I'm using Gtk on C, I need to have a GtkTextView in the middle of my window with many other widgets, I can't make the widget wrap lines. This is a very annoying behavior, anyone have any idea of what am I missing? This is the code I'm using to set it's properties:
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(commentsTextView),20);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(commentsTextView),290);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(commentsTextView),GTK_WRAP_WORD);
gtk_text_view_set_pixels_inside_wrap(GTK_TEXT_VIEW(commentsTextView),0);
gtk_widget_set_size_request(commentsTextView,300,300);
Edit:
I solved this in a different way, still the problem remains unsolved :S
Did you put your text view into a GtkScrolledWindow?
You also need to make sure there are spaces in your text if you want the wrapping to occur on words. If there are no spaces in your string that is inserted into the text view gtk won't know where 1 word ends and the other begins.
If you want the text to wrap on a string without spaces you could use GTK_WRAP_CHAR in place of GTK_WRAP_WORD.

Resources