Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Zero Values as Empty String?

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" />
like image 455
Caledonian Coder Avatar asked Mar 01 '12 15:03

Caledonian Coder


People also ask

How do you show a blank cell instead of 0?

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.

What is string format in Java?

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.


1 Answers

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}}" />
like image 79
patrick Avatar answered Sep 21 '22 05:09

patrick