String format in wpf - 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

Related

How to stop Windows Forms label text from rearranging itself

Today I tried using Windows Forms for the first time, which went pretty smoothly until I tried setting the text of a label to "0 lines", which was displayed as "lines 0" in the actual program. I tried a couple of other inputs for the Text property, and it seems the leading number in the label is always moved to the back. I couldn't find any property in the label describing some sort of numerical formatting, so I was wondering were this rearranging came from and what I could do to disable it. Thanks in advance!
screenshot of the input
screenshot of the result
As CodeCaster said in their comment, I simply had to change the RightToLeft property.

silverlight string format to limit the character count

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.

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 =)

Padding a numeric display in WPF

I've got a position readout that's very simple -- it's just a TextBlock with a Style applied to it. In that Style, I just set it like so (there are more properties than this, but I took them out for conciseness):
<Style x:Key="NumberStyle" TargetType="{x:Type TextBlock}">
<Setter Property="TextAlignment" Value="Center" />
</Style>
Now, I have one display that uses this style, and it will display a number from 0.0 to 30000.0. The problem is that since I'm centering the text, the number (if changing rapidly) jumps all over the place and it's a little disturbing. I'd like to format my string so that it won't do this.
I tried this ConverterParameter in XAML:
ConverterParameter='\{0:00000.0\}'
and while it does the padding properly, I'll get numbers like 00032.5. I then replaced the 0 with #, but that ends up behaving just like {0:0.0}. I looked at the MSDN docs and didn't see anything else that would help.
The only thing I can come up with is that I'd have to write a new IValueConverter to do this. In other words, in the Convert() method, I would have to take parameter and parse it for my own special character. And then when I detect this, replace the missing numbers with spaces.
However, what I am really trying to learn here is, can this be done by simply using a different character in the format string that I don't know about?
I think you need something like {0,7} (display argument on seven positions, padding to the left with spaces). However, even in this case, for the text not to jump, you need to use a monospaced font, or at least a font that has the width of the space character equal to the width of the digits.
Try {}{0,10:#,0} for a field of 10 characters.
Note, however, that this will give odd results if the font is not fixed-width. I tried it in Kaxaml, and it works, but the text doesn't line up with a proportional font.

Silverlight 3 - Control over wrapping in TextBox

Ok I have the following problem in Silverlight. I have a control with 2 columns. On the left is a stack panel with line numbers and on the right is a textBox.
So when I write in textBox without wrapping turned on I can simply create the right count of numbers on the left, because I'm searching for '\r' in text.
But when I turn on wrapping I have no control over the count of lines in textBox.
Is there a way to get to that count? Or a way to know which line in textBox is going to wrap?
I hope you can understand what I'm trying to do.
There's one way to do this. You can simulate the word wrap operation in the background using a TextBlock. Here is a good link of the complete solution to this problem.
Extended TextBox control with MaxLines property
Is it not possible to create your items in code before they are passed to the view. This would enable you to bind a list of items to a listview and style them as you wish.
You need to user a value converter to count the number of char / lines and then trim that number if you wish to. Unless you use fixed width, you can't really count or calculte in advancet the size, since each application might be displayed differently (due to different sizing option).
There are two great sample chapters on Windows Phone and Silverlight for Windows Phone on the LearningWindosPhone.com site. There is great Windows Phone Trainng material , and dont forget the Windows Phone Develoeprs Blog
Yes there is a way to get the number of lines occupied by the text in the textbox. It's not that simple though 'coz you have to simulate the behavior of the word wrap in order to count/predict the number of lines generated as a result of a word wrap. I have the solution described here in detail.

Resources