C# XAML StringFormat 格式化 Double 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1201793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:45:44  来源:igfitidea点击:

XAML StringFormat to format a Double value

c#wpfxaml

提问by Jobi Joy

I want to format my slider value to be 00:00 format. The below code worked for me, but what I wanted is 00:00 format. I am looking for a total XAML solution. I know that I can write a converter for this easily but wonder if there are any StringFormat way to make it.

我想将我的滑块值格式化为 00:00 格式。下面的代码对我有用,但我想要的是 00:00 格式。我正在寻找一个完整的 XAML 解决方案。我知道我可以轻松地为此编写一个转换器,但想知道是否有任何 StringFormat 方法来制作它。

Text="{Binding Value, ElementName=slider,StringFormat=\{0:00.00\}}" 

My question here is how can I get colon ':' instead of dot '.' ?

我的问题是我怎样才能得到冒号“:”而不是点“。” ?

回答by user112889

You can use the default String formatting options in the StringFormat as in regular C# code. Have a look at this page, you'll probably find what you need.

您可以像在常规 C# 代码中一样使用 StringFormat 中的默认字符串格式化选项。看看这个页面,你可能会找到你需要的。

回答by Alan Mendelevich

I don't think there's a real solution to this but if you can multiply your slider values by 100 you can just use 00:00 as your format string.

我认为没有真正的解决方案,但是如果您可以将滑块值乘以 100,则可以使用 00:00 作为格式字符串。

回答by Nate Zaugg

If you just want to put a :symbol in the format string, then you escape it with backslashes:

如果您只想:在格式字符串中放置一个符号,那么您可以使用反斜杠对其进行转义:

Text="{Binding Value, ElementName=slider,StringFormat=\{0:00\:00\}}"

But I think this is what you want to do:

但我认为这就是你想要做的:

<Slider Name="slider" Width="500" Height="30" Maximum="100" Minimum="0" />
<TextBlock Text="{Binding Path=Value, ElementName=slider, StringFormat={}{0:00\:00}}" />