I am using WPF databinding. I can format the number if I specify StringFormat={}{0:C}. However, if I use StringFormat={}{0:X2}, it will give give the following exception:
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.
Anyone gets any idea?
The hexadecimal format specifier only works with integer types. You’re trying to apply it to a floting point (
double) value. You’ll need to convert your bound value to an integer type (int,long, etc) if you want to format it as a hexadecimal value.