Caledonian Coder
Caledonian Coder

Reputation: 959

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" />

Upvotes: 17

Views: 9521

Answers (1)

patrick
patrick

Reputation: 16979

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}}" />

Upvotes: 7

Related Questions