【问题标题】:How to Hide 0 Value in Xaml Text Binding [duplicate]如何在 Xaml 文本绑定中隐藏 0 值
【发布时间】:2017-11-10 09:58:24
【问题描述】:

我正在编写 XamlPrint。每次可以在纸上打印某些东西时,xaml 都会定义 TextBlock。大多数情况下,如果 value 为 null,则绑定只返回 EmptyString,因此在我的打印中看不到它。它工作正常。

但对于价格,它会计算不同值的总和:

<TextBlock Text="{Binding ForfaitQuantity}" Width="100" Margin="1126,1110" TextAlignment="Right" />

这是一个文本块的示例,它为类型 Forfait 打印 Price。有时Forfait 是0。在这种情况下,我希望它显示StringEmpty 而不是0。

我该怎么做?转换器?还有什么?

【问题讨论】:

    标签: c# wpf xaml printing


    【解决方案1】:

    将此与文本绑定一起使用:

    public class ForfaitQuantityConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
             string stringValue = value as string;
            if (string.IsNullOrWhiteSpace(stringValue) || stringValue.Equals("0"))
            {
                return string.Empty;
            }
    
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
    
        }
    }
    

    【讨论】:

    • Value.Equals 会导致异常。
    • 我改变了一点我使用的转换器(值是字符串)然后转换它并检查值是否为“0”像一个魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2014-11-08
    • 2023-01-28
    相关资源
    最近更新 更多