Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top