将零值格式化为空字符串? [英] Formatting Zero Values as Empty String?

查看:31
本文介绍了将零值格式化为空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试 WPF 字符串格式时遇到了困难.我希望能够在基础值为零时使用空字符串格式化数据网格中的文本框列,并将所有其他值格式化为 0.000.但是,我的 XAML 似乎无法胜任,因为它显示 所有 值的空白,而不仅仅是零:

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.

非常感谢!

更新

这样做就行了:

<DataGridTextColumn Header="dL" Binding="{Binding Path=Value.DLHistoric, StringFormat=0.000;;#}" Width="Auto" />

推荐答案

IValueConverter 示例

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

这篇关于将零值格式化为空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆