如何在Xaml文本绑定中隐藏0值

时间:2017-11-10 09:58:25

标签: c# wpf xaml printing

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

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

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

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

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

1 个答案:

答案 0 :(得分: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)
    {

    }
}