WPF数据绑定StringFormat不支持十六进制格式?

时间:2012-10-16 02:46:10

标签: wpf data-binding hex string-formatting

我正在使用WPF数据绑定。如果我指定StringFormat={}{0:C},我可以格式化数字。但是,如果我使用StringFormat={}{0:X2},则会给出以下异常:

System.Windows.Data Error: 6 : 'StringFormat' converter failed to convert value '10' (type 'Double'); fallback value will be used, if available. BindingExpression:Path=Value; DataItem='Slider' (Name='SliderG'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Format specifier was invalid.
   at System.Number.FormatDouble(Double value, String format, NumberFormatInfo info)
   at System.Double.ToString(String format, IFormatProvider provider)
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'
'WpfApplication1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

任何人都有任何想法?

1 个答案:

答案 0 :(得分:1)

十六进制格式说明符仅适用于整数类型。您正尝试将其应用于浮点(double)值。如果要将其格式化为十六进制值,则需要将绑定值转换为整数类型(intlong等)。