在WPF字符串格式 [英] String format in wpf

查看:83
本文介绍了在WPF字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框 WPF 是在我的模型相关的浮点型变量

I have a TextBox on WPF that is related to the float variable in my model

继TextxBox:

 <TextBox Text="{Binding Position, StringFormat=f4}"/>

我想这将文本框后点最多显示4个号码。

I want that TextBox will display a maximum 4 numbers after the point.

所以我把的StringFormat = F4

但现在,即使我有一点后不到4个数字,当我有一个整数它显示在后点4位。

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.

例如,数字0是表明: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?

推荐答案

您可以使用的StringFormat =尝试{} {0:0} ####

此语法 {} 是由于我们设定了一个WPF属性等于包含花括号符号的字符串的事实。花括号符号是通过WPF以特定方式PTED间$ P $和不会作为PTED字符串的一部分际$ P $。如果没有 {} 的code不会编译。 {} 允许你将一个WPF设置为包含花括号符号的字符串值。

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.

您可以有例如看一下链接的字符串中的格式WPF和Silverlight

You can have for example a look at the link String formatting in WPF and Silverlight

这篇关于在WPF字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆