wpf 在 XAML 中,我想使用 StringFormat 来显示数值而不发生任何舍入。是否可以?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31814406/
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
In XAML I want to use StringFormat to display a numerical value without any rounding happening. Is it possible?
提问by tolism7
So in my XAML I have this:
所以在我的 XAML 中,我有这个:
<TextBlock Text="{Binding MyDecimalProperty, StringFormat={}{0:#.###}}"/>
When the value of MyDecimalProperty is for example 5.35570256 then the TextBlock value shown is 5.356
例如,当 MyDecimalProperty 的值为 5.35570256 时,显示的 TextBlock 值为 5.356
Is there a way to have the TextBlock show 5.355 instead (i.e. to truncate the number instead rounding it) using StringFormat?
有没有办法让 TextBlock 使用 StringFormat 显示 5.355(即截断数字而不是四舍五入)?
I know I can use a Value Converter but I wanted to know if I can use StringFormat or anything else defined in the XAML to achieve the same.
我知道我可以使用值转换器,但我想知道是否可以使用 StringFormat 或 XAML 中定义的任何其他内容来实现相同的目的。
Update: People keep wondering why I don't want to use a converter. Please read above in bold. Its not that I don't want to; I just wanted to know if I can do the same with StringFormat or any other XAML construct.
更新:人们一直想知道为什么我不想使用转换器。请以粗体阅读以上内容。不是我不想;我只是想知道我是否可以对 StringFormat 或任何其他 XAML 构造做同样的事情。
Thank you.
谢谢你。
采纳答案by ΩmegaMan
if I can use StringFormat or anything else defined in the XAML to achieve the same
如果我可以使用 StringFormat 或 XAML 中定义的任何其他内容来实现相同的
StringFormat is specific to the Binding Markup Extension(actually the binding base BindingBase.StringFormat Property (System.Windows.Data)) and ultimately uses the .Net string formatting such as ToString("G3")(see Standard Numeric Format Strings) which rounds and doesn't truncate; so it is not possible to override in Xaml such features.
StringFormat 特定于绑定标记扩展(实际上是绑定基础BindingBase.StringFormat 属性 (System.Windows.Data))并最终使用 .Net 字符串格式,例如ToString("G3")(请参阅标准数字格式字符串),它会四舍五入且不会截断;所以不可能在 Xaml 中覆盖这些功能。
Create another property on the viewmodel which is an associated stringtype which simply parrot's the value wanted, but truncated. Then bind as such.
在视图模型上创建另一个属性,它是一个关联string类型,它只是鹦鹉学舌的值,但被截断了。然后这样绑定。
ViewModel
视图模型
public decimal TargetDecimal
{
get { return _TargetDecimal; }
set { _TargetDecimal = value;
OnPropertyChanged("TargetDecimal");
OnPropertyChanged("TargetValueTruncated"); }
}
// No error checking done for example
public string TargetValueTruncated
{
get { return Regex.Match(_TargetDecimal.ToString(), @"\d+\.\d\d\d").Value; }
}
Xaml
xml
<TextBlock Text="{Binding TargetDecimal, StringFormat=Original: {0}}"/>
<TextBlock Text="{Binding TargetDecimal, StringFormat=Modified: {0:#.###}}"/>
<TextBlock Text="{Binding TargetValueTruncated, StringFormat=Truncated: {0}}"/>
Result
结果
回答by d.moncada
I do not think you can do this simply with using StringFormat. Is there a reason why you do not want to use a converter? (see below).
我认为你不能简单地使用 StringFormat 来做到这一点。您是否有不想使用转换器的原因?(见下文)。
Converter:
转换器:
public class TruncateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
double truncated;
if (Double.TryParse(value, out truncated))
{
return ((double)((int)(truncated * 1000.0))) / 1000.0;
}
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
Usage:
用法:
<TextBlock Text="{Binding MyDecimalProperty, Converter={StaticResource TruncateConverter}}"/>
回答by Il Vic
If you do not want to use a converter (I am not sure why, but you will have your reasons) you can consider changing the returned type of your binding source and then implementing IFormattableinterface.
如果您不想使用转换器(我不确定为什么,但您有自己的理由),您可以考虑更改绑定源的返回类型,然后实现IFormattable接口。
Take a look to this sample. First of all your MyDecimalPropertyshould not return a decimal anymore. You should change it in order to return a TruncatedDecimal. Here its code:
看看这个样本。首先,您MyDecimalProperty不应再返回小数。您应该更改它以返回TruncatedDecimal. 这是它的代码:
public class TruncatedDecimal : IFormattable
{
private decimal value;
public TruncatedDecimal(decimal doubleValue)
{
value = doubleValue;
}
public decimal Value
{
get
{
return value;
}
}
public string ToString(string format, IFormatProvider formatProvider)
{
int decimalDigits = Int32.Parse(format.Substring(1));
decimal mult = (decimal)Math.Pow(10, decimalDigits);
decimal trucatedValue = Math.Truncate(value * mult) / mult;
return trucatedValue.ToString(format, formatProvider);
}
}
Now in your XAML your can use the format string that you need:
现在在您的 XAML 中,您可以使用您需要的格式字符串:
<TextBlock Text="{Binding StringFormat={}N5}" Margin="5" FontSize="20" />
So, for example, if the DataContext is set in this way:
因此,例如,如果 DataContext 以这种方式设置:
DataContext = new TruncatedDecimal(new Decimal(.4997888869));
You will see 0.49978. I hope this solution will be suitable for your needs and can help you.
您将看到0.49978。我希望这个解决方案适合您的需求并可以帮助您。


