Need solution for Text trimming Problem - wpf

We are facing problem regarding Text trimming in TextBlock.
We have a Text "My very long label goes here" and we have to show it in following format:
[...g label goes here]
i.e right aligned and text should be trimmed from left (character ellipsis at left side)
We have used FlowDirection of TextBlock, ellipsis is correctly shown on the left side, but text is trimmed from the right side.
FlowDirection = "LeftToRight:"
[My very long lab...]
FlowDirection = "RightToLeft:"
[...My very long lab]
But we need:
[...g label goes here]
Please provide suggestions on this.

You don't mention how the TextBlock gets its string, but if its from a binding, have you considered using a value converter to just trim the string?
I guess this is also only applicable if your requirement is X number of characters before trimming, not if your requirement is Y number of pixels..

Related

Show "..." at the end of a label if it is too wide

I know it is possible to set MaximumSize to prevent a label from resizing off of a form, but is it possible to also clip the text if necessary?
For example, if you pass the sentence Hello, my name is Dave. to a label, making the width greater than the maximum, I'd like it to show Hello, my name is.... Thanks.
Use the AutoEllipsis property and don't forget to set AutoSize to False
MSDN says:
Gets or sets a value indicating whether the ellipsis character (...)
appears at the right edge of the Label, denoting that the Label text
extends beyond the specified length of the Label.

Telerik RadMaskedTextBox focus Mask cursor spacing

I keep running into this increasingly annoying nuance when using a RadMaskedTextBox wherein once the field gains focus, the cursor doesn't seem to position itself at the beginning of the field. Instead I get something like this;
Which would come from for example, the code below. Notice in the example the cursor starts 7 spaces from the beginning of the field. Which is exactly the number of #'s I have set in my Mask to allow numeric input of 7 characters as shown below.
<telerik:RadMaskedTextBox
Value="{Binding Path=Parameters.Blah, Mode=TwoWay}"
MaskType="Standard"
Placeholder=""
Mask="#######"/>
How can I get the cursor back to the beginning of the field when it receives focus while keeping my numeric input length? I have the suspicion I'm missing an inane detail here. Thanks in advance.
I'm not sure what you're asking for... but if you just want to get the cursor in the beginning of the radmaskedtextbox on focus (once you are focused, you can click wherever in the textbox to move the cursor) you should use this property:
SelectionOnFocus="CaretToBeginning"

How to change Tab character width in silverlight TextBox

The width of Tab (\t) character in silverlight TextBox is not equal to 4 spaces or 8 spaces. It's too short.
Is it possible to change the width of the TAB (\t) character displayed in a silverlight TextBox?
Note that I want to avoid replacing TABs with spaces.
Any ideas on how to do this?
Silverlight does not allow you to change the tab character length in a TextBox.
If your reading in a string (from a file or something) and setting the Text to it then if you look at your Text Property you'll see the escaped tab (\t). Searching for a \t is easy
TabTextBox.Text = TabTextBox.Text.Replace("\t", " ");
So this will replace all tabs with 4 spaces.
Besides pressing tab in a TextBox will not tab the text. It will focus to the next UIElement within the parent UIElement.
Or maybe on tab key press event, append the string literal to the text box. Similar to what MyK is suggesting.
If you're trying to get this to display, write a converter. the syntax is around--just grab an example, gut it, rename to something like 'tabstoptexttospacedtextconverter', add a reference to your local controls in app.xaml, then create an instance of it and give it an x:name to use it. Bind the data for your text box and assign it your new converter.
It'll be a little bit of hassle because you'll have to determine the appropriate width of the final display TB, and then probably use a converterparameter to make that work. But long story short, split on \t, then foreach(string str in splitSourceText) do something like this:
for (int i = 0; i < (str.Length % 8 > 0 ? str.Length % 8 : 8); i++) str+= " ";
You can limit the characters on the split string arrays with the parameter or split on \r\n first.

WPF TextBlock Cutoff

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.
You have a couple of options to control wrapping and cutting of text:
TextWrapping can be used to make the text flow to the next line
TextTrimming can be used to decide how to cut text that doesn't fit
TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.
TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.
If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described
I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).
If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

Resizing Labels

I have a chart in WPF with a lot of labels. The text on these labels is dynamically loaded and subject to change. If I set the width just to auto, then these labels may overlap, which makes the text unreadable.
The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. Now I want to adjust the text to the space which is available. If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip.
I can determine the length available for the string. But how can I determine the space a single letter will take? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly).
Any ideas?
If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word.
While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word.
Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML.
Start by obtaining the actual pixel width of the space available for the text.
Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character.
You could also look at changing the character sizes as well as reducing the label length.

Resources