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.
Related
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
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"
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.
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 =)
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.