Formatting Zero Values as Empty String?

一曲冷凌霜 提交于 2019-12-18 13:01:07

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/9518916/formatting-zero-values-as-empty-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!