在 WPF 中填充数字显示

发布于 2024-10-11 01:01:59 字数 871 浏览 3 评论 0原文

我有一个非常简单的位置读数——它只是一个应用了样式的 TextBlock。在该样式中,我只是这样设置(还有比这更多的属性,但为了简洁起见,我将它们删除了):

    <Style x:Key="NumberStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="TextAlignment" Value="Center" />
    </Style>

现在,我有一个使用此样式的显示器,它将显示从 0.0 到 30000.0 的数字。问题是,由于我将文本居中,数字(如果变化很快)会到处乱跳,这有点令人不安。我想格式化我的字符串,这样它就不会这样做。

我在 XAML 中尝试了这个 ConverterParameter:

ConverterParameter='\{0:00000.0\}'

虽然它正确填充,但我会得到像 00032.5 这样的数字。然后,我将 0 替换为 #,但最终的行为就像 {0:0.0} 一样。我查看了 MSDN 文档,没有看到任何其他内容帮助。

我唯一能想到的是我必须编写一个新的 IValueConverter 来执行此操作。换句话说,在 Convert() 方法中,我必须获取参数并解析它以获取我自己的特殊字符。然后当我检测到这一点时,用空格替换丢失的数字。

但是,我在这里真正想学习的是,可以通过简单地在格式字符串中使用我不知道的不同字符来完成此操作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-10-18 01:01:59

我认为你需要类似 {0,7} (在七个位置显示参数,用空格向左填充)。然而,即使在这种情况下,为了使文本不跳转,您需要使用等宽字体,或者至少使用空格字符宽度等于数字宽度的字体。

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.

两相知 2024-10-18 01:01:59

尝试 {}{0,10:#,0} 输入 10 个字符的字段。

但请注意,如果字体不是固定宽度,这将产生奇怪的结果。我在 Kaxaml 中尝试过,它有效,但文本未与比例字体对齐。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文