Set Listview visibility to true only if is different of null

Hey people, first of all sorry for possible english errors!

My simple question is: How to hide listview if there is null or count is 0?
For example i can have a profile class that can contains (or no) posts.

If posts is 0, i set my listview item source to null, and i want to hide the list, but how to bind xaml visibility to null or something like?

Thanks.
Using XAML and MVVM

Posts

  • IvanHarrisIvanHarris USMember ✭✭✭

    List listviewObjects = new List();
    if (listviewObjects == null)
    listView.IsVisible = false;

  • LucasPereiraLucasPereira USMember

    @IvanHarris said:
    List listviewObjects = new List();
    if (listviewObjects == null)
    listView.IsVisible = false;

    How to do this on XAML and MVVM?

  • IvanHarrisIvanHarris USMember ✭✭✭

    Good luck with that. I don't know. Sorry.

    Why can't you use the above code with XAML? you need to do an if statement to test if listviewobjects are null or count is 0, so you need to put that code in the codebehind (PageName.xaml.cs file) anyways.

  • JacobPoulsen_JacobPoulsen_ DKMember ✭✭✭

    Hi @LucasPereira

    To do this using a Binding in XAML you could very easily create a bool property in your ViewModel to bind the IsVisible property of your ListView to.

    Then whenever you refresh your ListView Items, you do a quick check if there are more than 0 items in your list.
    If there is, you can set the bool property to true, if not you set it to false, causing the Listview to show or hide, based on the number of items in the list.

    Please let me know if you need further assistance.

    (You could also use a Binding Converter as shown here: https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/ )

  • LucasPereiraLucasPereira USMember

    @IvanHarris said:
    Good luck with that. I don't know. Sorry.

    Why can't you use the above code with XAML? you need to do an if statement to test if listviewobjects are null or count is 0, so you need to put that code in the codebehind (PageName.xaml.cs file) anyways.

    I trying to have a "pure" MVVM approach with no codes in XAML.cs, but thank you!

    @JacobPoulsen_ said:
    Hi @LucasPereira

    To do this using a Binding in XAML you could very easily create a bool property in your ViewModel to bind the IsVisible property of your ListView to.

    Then whenever you refresh your ListView Items, you do a quick check if there are more than 0 items in your list.
    If there is, you can set the bool property to true, if not you set it to false, causing the Listview to show or hide, based on the number of items in the list.

    Please let me know if you need further assistance.

    (You could also use a Binding Converter as shown here: https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/ )

    Thank you Jacob!
    I think the Binding Converters will resolve my problem

Sign In or Register to comment.