question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IsVisible Property not update in ListView

See original GitHub issue

Description

When I want to change IsVisible property on ListView, via Binding, at first and second time, when im chaning value of binded bool, i can’t see any changes. When i change binded for the third and next times, it works properly.

Steps to Reproduce

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:MauiApp1.ViewModels"
             x:Class="MauiApp1.Views.LibraryPage"
             Title="LibraryPage"
             BackgroundColor="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource bg00dp}}">
    <ContentPage.BindingContext>
        <vm:LibraryViewModel/>
    </ContentPage.BindingContext>
    <ScrollView>
        <VerticalStackLayout>
            <Grid Padding="5"
              BackgroundColor="{StaticResource bg01dp}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>

                <ImageButton Source="refresh_line.png"
                         WidthRequest="36"
                         HeightRequest="36"
                         VerticalOptions="CenterAndExpand"
                         HorizontalOptions="EndAndExpand"
                         Command="{Binding RefreshCommand}"
                         Grid.Column="0"/>
            </Grid>

            <VerticalStackLayout>
                <VerticalStackLayout Padding="5">
                    <HorizontalStackLayout Spacing="10"
                                           VerticalOptions="CenterAndExpand">
                        <Image Source="download_2_line.png"
                               WidthRequest="36"
                               HeightRequest="36"
                               />
                        <Label Text="Downloaded"
                               VerticalOptions="Center" 
                               HorizontalOptions="StartAndExpand"
                               FontSize="Title"
                               FontAttributes="Bold"/>
                    </HorizontalStackLayout>
                    <VerticalStackLayout.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding DownloadedVisibleCommand}"/>
                    </VerticalStackLayout.GestureRecognizers>
                </VerticalStackLayout>
                <ListView IsVisible="{Binding IsDownloadedVisible}"
                          ItemsSource="{Binding DownloadedTracks}"
                          SelectionMode="None"
                          Margin="10">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <HorizontalStackLayout Spacing="10">
                                        <Image Source="{Binding ThumbSource}"
                                           WidthRequest="50"
                                           HeightRequest="50"/>
                                        <VerticalStackLayout VerticalOptions="CenterAndExpand">
                                            <Label Text="{Binding title}"
                                               LineBreakMode="TailTruncation"
                                               MaximumWidthRequest="300"/>
                                            <Label Text="{Binding author}"
                                               TextColor="{StaticResource Gray400}"/>
                                        </VerticalStackLayout>
                                    </HorizontalStackLayout>
                                    <Grid.GestureRecognizers>
                                        <TapGestureRecognizer NumberOfTapsRequired="1"
                                                      Command="{Binding Source={RelativeSource AncestorType={x:Type vm:LibraryViewModel}}, Path=PlaySelectedCommand}"
                                                      CommandParameter="{Binding .}"/>
                                    </Grid.GestureRecognizers>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </VerticalStackLayout>
            <VerticalStackLayout>
                <Grid Padding="5">
                    <HorizontalStackLayout Spacing="10"
                                           VerticalOptions="CenterAndExpand">
                        <Image Source="playlist_2_line.png"
                               WidthRequest="36"
                               HeightRequest="36"/>
                        <Label Text="Playlists"
                               FontSize="Title"
                               FontAttributes="Bold"
                               VerticalOptions="CenterAndExpand"
                               HorizontalOptions="StartAndExpand"/>
                    </HorizontalStackLayout>
                    
                    <ImageButton Source="add_line.png"
                                 WidthRequest="36"
                                 HeightRequest="36"
                                 Command="{Binding AddPlaylistCommand}"
                                 HorizontalOptions="EndAndExpand"/>
                    <Grid.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding PlaylistsVisibleCommand}"/>
                    </Grid.GestureRecognizers>
                </Grid>
                <ListView ItemsSource="{Binding Playlists}"
                          SelectionMode="None"
                          Margin="10"
                          IsVisible="{Binding IsPlaylistsVisible, Mode=TwoWay}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <HorizontalStackLayout Spacing="10"
                                                           VerticalOptions="CenterAndExpand">
                                        <Image Source="rhythm_line.jpg"
                                               WidthRequest="50"
                                               HeightRequest="50"/>
                                        <VerticalStackLayout>
                                            <Label Text="Playlist Name"/>
                                        </VerticalStackLayout>
                                    </HorizontalStackLayout>
                                    <Grid.GestureRecognizers>

                                    </Grid.GestureRecognizers>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </VerticalStackLayout>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>
using AngleSharp.Browser;
using MauiApp1.LocalDatabase;
using CommunityToolkit.Maui.Views;

using MauiApp1.Model;
using MauiApp1.Services;
using MauiApp1.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp1.ViewModels
{
    internal class LibraryViewModel : BaseViewModel
    {
        public ObservableCollection<DownloadedTrack> DownloadedTracks { get; set; }
        public ObservableCollection<Playlist> Playlists { get; set; }

        public Command DownloadedVisibleCommand { get; set; }
        public Command PlaylistsVisibleCommand { get; set; }
        public Command RefreshCommand { get; set; }
        public Command AddPlaylistCommand { get; set; }
        public Command<DownloadedTrack> PlaySelectedCommand { get; set; }

        private bool _isDownloadedVisible;

        public bool IsDownloadedVisible
        {
            get { return _isDownloadedVisible; }
            set
            { 
                _isDownloadedVisible = value;
                OnPropertyChanged(nameof(IsDownloadedVisible));
            }
        }

        private bool _isPlaylistsVisible;

        public bool IsPlaylistsVisible
        {
            get { return _isPlaylistsVisible; }
            set
            { 
                _isPlaylistsVisible = value;
                OnPropertyChanged(nameof(IsPlaylistsVisible));
            }
        }



        public LibraryViewModel()
        {
            //IsDownloadedVisible = false;
            //IsPlaylistsVisible = false;
            DownloadedVisibleCommand = new(() => IsDownloadedVisible = !IsDownloadedVisible);
            PlaylistsVisibleCommand = new(() => IsPlaylistsVisible = !IsPlaylistsVisible);

            FillDownloadedTracks();
            GetPlaylists();
            RefreshCommand = new(FillDownloadedTracks);
            PlaySelectedCommand = new((DownloadedTrack track) => GlobalData.GlobalPlayer.ChangeSource(track.local_path, track.title, track.author, track.youtube_id));
            
            AddPlaylistCommand = new(() =>
            {
                GlobalData.LibraryPage.ShowPopupPl();
            });

            Playlists ??= new();

            Playlists.Add(new());
            Playlists.Add(new());
            Playlists.Add(new());
        }

        private async void FillDownloadedTracks()
        {
            DownloadedTracks ??= new();

            DownloadedTracks.Clear();
            LocalDatabaseService local_db = new();
            var a = await local_db.GetDownloadedTracks();


            foreach (var item in a)
            {
                DownloadedTracks.Add(new DownloadedTrack(item));
            }
        }

        private void GetPlaylists()
        {
            Playlists ??= new();
            //Playlists.Clear();

            //LocalDatabaseService local_db = new();
            //var a = local_db.GetAllPlaylists();

            //foreach (var item in a)
            //{
                //Playlists.Add(item);
            //}
        }
    }
}

Link to public reproduction project repository

https://github.com/rudziktv/MusicAppMAUI

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 12.0 - API 31

Did you find any workaround?

When you bind bool, for other control, before listview, it works properly always.

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
rudziktvcommented, Sep 26, 2022

I tried

dispatcher = Shell.Current.CurrentPage.Dispatcher;

DownloadedVisibleCommand = new(() => dispatcher.Dispatch(() => IsDownloadedVisible = !IsDownloadedVisible));
PlaylistsVisibleCommand = new(() => dispatcher.Dispatch(() => IsPlaylistsVisible = !IsPlaylistsVisible));

it’s crashes app and

dispatcher = Shell.Current.Dispatcher;

DownloadedVisibleCommand = new(() => dispatcher.Dispatch(() => IsDownloadedVisible = !IsDownloadedVisible));
PlaylistsVisibleCommand = new(() => dispatcher.Dispatch(() => IsPlaylistsVisible = !IsPlaylistsVisible));

and it not works.

I did’t add that to constructor, because I use ContentPage.BindingContext, which not allows to give ctor arguments. To workaround I use empty <Label IsVisible="{Binding IsPlaylistVisible}"/>, before <ListView> and it works fine.

0reactions
homeyfcommented, Jul 21, 2023

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 3.0(net8). Can repro on Android platform with sample project. https://github.com/rudziktv/MusicAppMAUI image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Listview not updated when isVisible is set to true
I'm trying to make a ListView with clickable rows. When you click on a row it sets a child stacklayout to visibility true....
Read more >
Binding IsVisible property ListView Datatemplate to ...
Hi guys,. Got a Datatemplate list that I want to toggle the buttons on and off depending on something being shown in the...
Read more >
ListView does not resize itself after an item is invisible
The items become invisible, there is no problem but occupied places by items stay still after we set invisible. We use MVVM and...
Read more >
Searchable ListView? - Forums
I have a bound list, where each item has an isVisible property updated by search and other factors. I added this to the...
Read more >
StackLayout isVisible property not working in Xamarin Forms
Try this public void OnButtonClick(object sender, EventArgs args){ popupLayout.IsVisible = true; popupLayout.ForceLayout(); }.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found