如果observablecollection为null,如何隐藏Textblock

时间:2015-03-18 17:02:16

标签: xaml

如果使用xaml代码绑定observablecollection包含空,是否可以隐藏TextBlock。?

1 个答案:

答案 0 :(得分:3)

XAML

<TextBlock Visibility="{Binding ElementName=Labour,Path=Items.Count, Converter={StaticResource ZeroToVisibilityConverter}}" TextWrapping="Wrap" Text="There is no data on this date" Margin="314,200,0,0" FontFamily="Arial" FontSize="14" Height="37" VerticalAlignment="Top" HorizontalAlignment="Left" Width="188"/>

转换器

public class ZeroToVisibilityConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return int.Parse(value.ToString()) == 0 ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}