silverlight string format to limit the character count - silverlight

I am using the SubString method to limit the character count. Is this possible to do in XAML? If so, how do you set the format string to limit the character count in XAML as in String.Format
For example:
My Big text Message -> My Big text....

Using the TextTrimming property of TextBlock in Silverlight 4 should work:
<TextBlock TextTrimming="WordEllipsis"/>
What's nice about this is that it will use actual text measurements instead of arbitrary character counts, since obviously not all characters are the same width.

Related

String format in wpf

I have a TextBox on WPF that is related to the float variable in my model
Following the TextxBox:
<TextBox Text="{Binding Position, StringFormat=f4}"/>
I want that TextBox will display a maximum 4 numbers after the point.
So I put StringFormat=f4.
But now, even when I have less than 4 numbers after the point and when I have a whole number it displays with 4 digits after the point.
For example, the number 0 is shows that: 0.0000
I want as long as it did not pass the four numbers, display it in a normal way, how can I do this?
you could try with StringFormat="{}{0:0.####}"
This syntax with {} is due to the fact that we set a WPF property equal to a string that contains curly bracket symbols. Curly bracket symbols are interpreted by WPF in a particular way and would not be interpreted as part of the string. Without the {} the code would not compile. {} allows you to set a WPF to a string value that contains curly bracket symbols.
You can have for example a look at the link String formatting in WPF and Silverlight
Take a look at this link about Custom Numeric Format Strings. I think this is what you might be looking for.
Or alternatively, try this;
<TextBox Text="{Binding Position, StringFormat='{}{0:#,0000}'}"/>
Hope this helps! :)
EDIT:
This previous question might help also;
WPF binding StringFormat syntax

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.

silverlight textbox with equal space between leters

I'm developing an application for a bank, and I need a textbox for entering the money, like
My Idea was to create a textbox that has as a background the image of the grid, and than I just set the text size such that there is only a character in each box. but writing iiiii(5 characters) is as long as wwww (4 characters). Can I set a font or a character spacing such hat i ensure that the characters writen in the textbox will appear in separate boxes.
Ps: there are other similar boxes for name, so I don't inpu only digits.
You could use monospace (fixed width) font, Courier for example. Or you could create custom control with TextBox for each character, but in this case you would have to implement big chunk of custom logic.
Using a Glyphs control, you can set indices for exact spacing of the characters of the UnicodeString property. Check this for details: http://msdn.microsoft.com/en-us/library/bb979862%28VS.95%29.aspx.
You can set the value for the UnicodeString in the code-behind or viewmodel if you are using MVVM.

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