I'm struggling with my first foray into WPF string formatting. I'd like to be able to format a textbox column in a data grid with an empty string when the underlying value is zero and format all other values as 0.000. However, my XAML doesn't seem to be up to the job as it shows blanks for all values and not just for zeros:
<DataGridTextColumn Header="dL" Binding="{Binding Path=Value.DLHistoric, StringFormat='{}{0.000;; }'" Width="Auto" />
I am using the semicolon operator as described here and have added a space after the second semicolon to get the empty string.
Many thanks!
Update
This does the trick:
<DataGridTextColumn Header="dL" Binding="{Binding Path=Value.DLHistoric, StringFormat=0.000;;#}" Width="Auto" />
Under Display options for this worksheet, select a worksheet, and then do one of the following: To display zero (0) values in cells, select the Show a zero in cells that have zero value check box. To display zero values as blank cells, clear the Show a zero in cells that have zero value check box.
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
Example IValueConverter
[ValueConversion(typeof(string), typeof(string))]
public class StringToFeetAndInches : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = value as string;
if (string.IsNullOrEmpty(str)) return str;
str = str.Insert(1, "'");
str = str + "\"";
return str;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return 0;
}
}
<UserControl.Resources>
<NS:StringToFeetAndInches x:Key="cStringToFeetAndInches"/>
</UserControl.Resource>
<TextBlock Text="{Binding Path=Height, Converter={StaticResource cStringToFeetAndInches}}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With