Showing empty view when ListView is empty in Xamarin.Forms RRS feed

  • Question

  • User285863 posted

    Is there a way to replicate this in forms?I want to show a message like "empty list" when listveiw is empty.

    Wednesday, January 11, 2017 11:55 PM

All replies

  • User208001 posted

    It could be that you put a label in the middle (in invisible mode), at the moment of loading the information if nothing was found, shown as visible (label) and the Liistview as invisible

    Thursday, January 12, 2017 12:07 AM
  • User2148 posted

    I think yes. You could have a label with "EMPTY LIST" that has IsVisible binded to ListViewItem.Count (Also listview should have the IsVisible property binded with ListViewItem.Count...), then, if Count > 0, Label is not visible (and list is visible). if Count == 0, Label is visible and ListView is not visible... hope to be clear also without a line of code...

    Thursday, January 12, 2017 12:09 AM
  • User285863 posted

    @AlessandroCaliaro I understand the logic but can you give an example?

    Thursday, January 12, 2017 12:35 AM
  • User2148 posted

    I try to write some code code in next hours. Be patience

    Thursday, January 12, 2017 6:24 AM
  • User2148 posted

    ok @ScottJean-Philippe


    here you can find a demo (born for other things but added your problem).

    You found 3 items in your listview. If you press "Description", you will popup a dialog. Select Delete so you delete the row. Delete all rows. The "The list is empty" label will appears.

    Let me know Have a nice day

    Thursday, January 12, 2017 10:34 AM
  • User285863 posted

    @AlessandroCaliaro So I looked at the code and it's a bit overwhelming heh...but it's what I asked for.I'm going try and see if I can dissect it on my own

    Thursday, January 12, 2017 7:43 PM
  • User2148 posted

    It's not so difficult... Do you know mvvm? feel free to ask if you need help

    Thursday, January 12, 2017 8:08 PM
  • User285863 posted

    @AlessandroCaliaro not really

    Thursday, January 12, 2017 8:24 PM
  • User2148 posted

    take a look at @JamesMontemagno video

    It's amazing


    Thursday, January 12, 2017 8:26 PM
  • User364053 posted

    Hey guys... This thread is old, but maybe it'll help someone: What I end up doing was

    if ((MyList.ItemsSource as List<MyType>).Count == 0)
                        MyList.IsVisible = false;
                        EmptyMessage.IsVisible = true;

    Where the ListView MyList and the Label EmptyMessage defined in the xaml, and MyType is the type of the items used to create MyList.

    It works perfectly fine for me :smile:

    Thursday, January 4, 2018 4:31 PM
  • User369595 posted

    @Ofaim its working for me thanks!

    Saturday, February 23, 2019 9:55 AM
  • User393915 posted

    Is anybody looking for more MVVM based implementation, here is how you do: Have one HasItems property in your ViewModel which contains the list. Set the HasItems = Items.Any(); whenever refreshing the list items, bind the IsVisible of list directly to HasItems and inversely bind the label of StackLayout with HasItems. <ListView x:Name="ItemsListView" IsVisible="{Binding HasItems}" And here is empty view: <StackLayout IsVisible="{Binding HasItems, Converter={StaticResource cnvReveresBoolConverter}}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

    cnvReveresBoolConverter is my custom converter which reverses the bool value, you can use data triggers.

    <StackLayout IsVisible="False"> <StackLayout .Triggers> <DataTrigger TargetType="StackLayout " Binding="{Binding HasItems}" Value="False"> <Setter Property="IsVisible" Value="True"/> </DataTrigger> .......

    Friday, May 29, 2020 12:55 PM