Binding Visibility property to item count in a GridView RRS feed

  • Question

  • Hi there,

    I have a GridView and a TextBlock. The GridView's ItemSource is binded to a CollectionViewSource, which's Source ist binded to a Vector in code-behind. The TextBlock should be visible whenever there are no items in the GridView, but I can't really get this to work.

    I tried binding the Visibility property of the TextBlock to the CollectionViewSource using a converter that examined the Size property of the CollectionViewSource, but this converter was only called once, when Setting the Source of the CollectionViewSource, but not afterwards, when the Source changed.

    I also tried setting the DataContext of the TextBlock directly to the Vector used as a Source for the CollectionViewSource and making the converter examine that one's Size property, but this also gets called just once in the beginning.

    Is there a better way to do this?

        <CollectionViewSource x:Name="ViewSource" IsSourceGrouped="True" Source="{Binding}" ItemsPath="songs"/> 
            <local:Converter x:Key="ItemToVisibilityConverter"/>
        <GridView ItemsSource="{Binding Source={StaticResource ViewSource}}">
        <TextBlock x:Name="EmptyText"  Visibility="{Binding  Converter={StaticResource ItemToVisibilityConverter}, Source={StaticResource ViewSource}}"/>
    public ref class Converter sealed : Windows::UI::Xaml::Data::IValueConverter
    		virtual Platform::Object^ Convert(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,	Platform::Object^ parameter, Platform::String^ language)
    			if (!value) return Windows::UI::Xaml::Visibility::Visible;
    			else if (dynamic_cast<Windows::UI::Xaml::Data::CollectionViewSource^>(value)->View->Size == 0) return Windows::UI::Xaml::Visibility::Visible;
    			else return Windows::UI::Xaml::Visibility::Collapsed;
    		virtual Platform::Object^ ConvertBack(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,	Platform::Object^ parameter, Platform::String^ language)
    			throw ref new Platform::NotImplementedException();

    Thanks in advance

    Saturday, August 2, 2014 3:28 PM

All replies

  • Hi,

    You're binding the ViewSource element to the TextBlock, you should know the ViewSource element doesn't change after instanciation that's why the converter is only called once, try to bind it the Count property of your list, (for example ViewSource.Source.Count property), and use a Converter for numbers ( return (value==0)?Visible : Collapsed ).

    since the source is binded to {Binding} I think you only have to do something like this :

    <TextBlock x:Name="EmptyText"  Visibility="{Binding, path="Count",  Converter={StaticResource CountToVisibilityConverter}}"/>

    I Wonder if your old binding actually works since ViewSource in the code is a Name and you're using it as a Key.

    I hope it helps,


    Saturday, August 2, 2014 9:25 PM
  • If I do so, all I get is

    Error: BindingExpression path error: 'Size' property not found on 'Windows.Foundation.Collections.IObservableVector`1<...>'. BindingExpression: Path='Size' DataItem='Windows.Foundation.Collections.IObservableVector`1<...>'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='EmptyText'); target property is 'Visibility' (type 'Visibility')

    But I got this to work now by binding to the CurrentItem-property of the CollectionViewSource and hiding the TextBlock whenever that one is null.

    Thursday, August 7, 2014 5:23 PM
  • Now I found out this also doesn't always update. So I tried the following:

    <TextBlock x:Name="EmptyText" Visibility="{Binding Path=Source.Size, ElementName=ViewSource, Converter={StaticResource ItemToVisibilityConverter}}"/>

    Which Returns the Error

    Error: BindingExpression path error: 'Size' property not found on 'Windows.Foundation.Collections.IObservableVector`1<...>'. BindingExpression: Path='Source.Size' DataItem='Windows.UI.Xaml.Data.CollectionViewSource'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='EmptyText'); target property is 'Visibility' (type 'Visibility')

    Eventhough, according to msdn, IObservableVector inherits form IVector, which has a Size-property. What am I doing wrong?

    Friday, August 8, 2014 12:32 PM