Xamarin.forms: [Bug] Xamarin Forms - ListView: IsVisible binding to an invisible item inside a list view is not working after scrolling to show that item

Created on 27 Jun 2019  Â·  18Comments  Â·  Source: xamarin/Xamarin.Forms

Hello,

I'm running into a very annoying problem, what I need is simple but I'm getting an unexpected problem, here is what I'm trying to do:

I have a listview containing 2 labels in the viewcell, both labels show the same field but the difference is that the first is visible and the font is normal and the second is invisible and the font is bold, what I wanted to do is when I click on the item to change the property named: "Selected" from false to true and vise versa so that the bold label show/hide to inform the user that the item is selected.

In order to achieve this I wrote my code and I deploy the simple app on my android device and it worked like a charm, but after that I noticed that if I scroll the listview to an item that was not shown on the screen and click on that item, the first normal label disappeared which is an expected behavior but the second bold label doesn't show

The following is my code:

testVisibilityProblem.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:local="clr-namespace:CPALMSStandardsViewer;assembly=CPALMSStandardsViewer"
             xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
             xmlns:converters="clr-namespace:CPALMSStandardsViewer.Converters"
             xmlns:Helpers="clr-namespace:CPALMSStandardsViewer.Helper"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="CPALMSStandardsViewer.Views.testVisibilityProblem">
    <Grid>
        <ListView Margin="10,0,10,0"
                  BackgroundColor="White"
                  SeparatorColor="Gray"
                  ItemsSource="{Binding CustomEntities}"
                  VerticalOptions="Fill"
                  SelectionMode="None"
                  HasUnevenRows="True">
            <ListView.Behaviors>
                <b:EventToCommandBehavior EventName="ItemTapped" 
                                          Command="{Binding SelectCustomEntity}"
                                          EventArgsConverter="{converters:ItemTappedEventArgsConverter}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Label  Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" IsVisible="{Binding IsSelected, Converter={Helpers:InverseBoolConverter}}"></Label>
                            <Label  Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" FontAttributes="Bold" IsVisible="{Binding IsSelected}"></Label>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</ContentPage>

testVisibilityProblemViewModel.cs

using CPALMSStandardsViewer.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CPALMSStandardsViewer.ViewModels
{
    public class testVisibilityProblemViewModel : ViewModelBase
    {
        public ObservableCollection<CustomEntity> CustomEntities { get; set; }

        public testVisibilityProblemViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            Title = "Test Visibility Problem!";
            CustomEntities = new ObservableCollection<CustomEntity>();
            for (int i = 0; i < 50; i++)
            {
                CustomEntities.Add(new CustomEntity() { Name = "Item " + i, IsSelected = false });

            }
        }

        private DelegateCommand<CustomEntity> _SelectCustomEntity;
        public DelegateCommand<CustomEntity> SelectCustomEntity => _SelectCustomEntity ?? (_SelectCustomEntity = new DelegateCommand<CustomEntity>(ExecuteSelectCustomEntityCommand));

        private void ExecuteSelectCustomEntityCommand(CustomEntity paramData)
        {
            paramData.IsSelected = !paramData.IsSelected;
        }


    }
}

CustomEntity.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace CPALMSStandardsViewer.Models
{
    public class CustomEntity : INotifyPropertyChanged
    {

        public virtual string Name { get; set; }

        private bool _IsSelected;
        public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnPropertyChanged("IsSelected"); } }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

I searched a lot concerning this problem and all that I found is something related to the OnPropertyChanged(), someone misses use the observable collection, other one misses the use of OnPropertyChanged(), other one used it in wrong way...

But my problem is different, it's not about binding because the top items worked as expected and also as I already mentioned the first label becomes hidden which is expected, my problem is that when you turn the IsVisible from false to true for an item that was not rendered on the screen, it simply not working.

I may know the problem but I tried a lot to get it solved but none of the solutions I tried work for me.

Please note that UWP worked as expected, my problem is on the Android platform.

Please HELP!

listview 5 Android bug

Most helpful comment

Just updated to XF 4.2 and it seems to fix the issue.

All 18 comments

In addition to my previous entry, please find attached Files.zip
Files.zip

Facing the same issue after updating to latest version of xamarin.forms.
The listview item IsVisible binding is not updating UI properly

In addition to my previous entry, please find attached Files.zip
Files.zip

Hello,

I'm running into a very annoying problem, what I need is simple but I'm getting an unexpected problem, here is what I'm trying to do:

I have a listview containing 2 labels in the viewcell, both labels show the same field but the difference is that the first is visible and the font is normal and the second is invisible and the font is bold, what I wanted to do is when I click on the item to change the property named: "Selected" from false to true and vise versa so that the bold label show/hide to inform the user that the item is selected.

In order to achieve this I wrote my code and I deploy the simple app on my android device and it worked like a charm, but after that I noticed that if I scroll the listview to an item that was not shown on the screen and click on that item, the first normal label disappeared which is an expected behavior but the second bold label doesn't show

The following is my code:

testVisibilityProblem.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:local="clr-namespace:CPALMSStandardsViewer;assembly=CPALMSStandardsViewer"
             xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
             xmlns:converters="clr-namespace:CPALMSStandardsViewer.Converters"
             xmlns:Helpers="clr-namespace:CPALMSStandardsViewer.Helper"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="CPALMSStandardsViewer.Views.testVisibilityProblem">
    <Grid>
        <ListView Margin="10,0,10,0"
                  BackgroundColor="White"
                  SeparatorColor="Gray"
                  ItemsSource="{Binding CustomEntities}"
                  VerticalOptions="Fill"
                  SelectionMode="None"
                  HasUnevenRows="True">
            <ListView.Behaviors>
                <b:EventToCommandBehavior EventName="ItemTapped" 
                                          Command="{Binding SelectCustomEntity}"
                                          EventArgsConverter="{converters:ItemTappedEventArgsConverter}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Label  Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" IsVisible="{Binding IsSelected, Converter={Helpers:InverseBoolConverter}}"></Label>
                            <Label  Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" FontAttributes="Bold" IsVisible="{Binding IsSelected}"></Label>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</ContentPage>

testVisibilityProblemViewModel.cs

using CPALMSStandardsViewer.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CPALMSStandardsViewer.ViewModels
{
    public class testVisibilityProblemViewModel : ViewModelBase
    {
        public ObservableCollection<CustomEntity> CustomEntities { get; set; }

        public testVisibilityProblemViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            Title = "Test Visibility Problem!";
            CustomEntities = new ObservableCollection<CustomEntity>();
            for (int i = 0; i < 50; i++)
            {
                CustomEntities.Add(new CustomEntity() { Name = "Item " + i, IsSelected = false });

            }
        }

        private DelegateCommand<CustomEntity> _SelectCustomEntity;
        public DelegateCommand<CustomEntity> SelectCustomEntity => _SelectCustomEntity ?? (_SelectCustomEntity = new DelegateCommand<CustomEntity>(ExecuteSelectCustomEntityCommand));

        private void ExecuteSelectCustomEntityCommand(CustomEntity paramData)
        {
            paramData.IsSelected = !paramData.IsSelected;
        }


    }
}

CustomEntity.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace CPALMSStandardsViewer.Models
{
    public class CustomEntity : INotifyPropertyChanged
    {

        public virtual string Name { get; set; }

        private bool _IsSelected;
        public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnPropertyChanged("IsSelected"); } }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

I searched a lot concerning this problem and all that I found is something related to the OnPropertyChanged(), someone misses use the observable collection, other one misses the use of OnPropertyChanged(), other one used it in wrong way...

But my problem is different, it's not about binding because the top items worked as expected and also as I already mentioned the first label becomes hidden which is expected, my problem is that when you turn the IsVisible from false to true for an item that was not rendered on the screen, it simply not working.

I may know the problem but I tried a lot to get it solved but none of the solutions I tried work for me.

Please note that UWP worked as expected, my problem is on the Android platform.

Please HELP!

Is this issue fixed??

Nope the issue still open but I used telerik xamarin listview instead and it's working as expected.

Sent from my Samsung Galaxy smartphone.

-------- Original message --------
From: Akhil VS notifications@github.com
Date: 7/5/19 11:42 AM (GMT+02:00)
To: "xamarin/Xamarin.Forms" Xamarin.Forms@noreply.github.com
Cc: Fadl Assaad AssaadF@paxosoft.com, Author author@noreply.github.com
Subject: Re: [xamarin/Xamarin.Forms] [Bug] Xamarin Forms - ListView: IsVisible binding to an invisible item inside a list view is not working after scrolling to show that item (#6683)

In addition to my previous entry, please find attached Files.zip
Files.ziphttps://github.com/xamarin/Xamarin.Forms/files/3334400/Files.zip

Hello,

I'm running into a very annoying problem, what I need is simple but I'm getting an unexpected problem, here is what I'm trying to do:

I have a listview containing 2 labels in the viewcell, both labels show the same field but the difference is that the first is visible and the font is normal and the second is invisible and the font is bold, what I wanted to do is when I click on the item to change the property named: "Selected" from false to true and vise versa so that the bold label show/hide to inform the user that the item is selected.

In order to achieve this I wrote my code and I deploy the simple app on my android device and it worked like a charm, but after that I noticed that if I scroll the listview to an item that was not shown on the screen and click on that item, the first normal label disappeared which is an expected behavior but the second bold label doesn't show

The following is my code:

testVisibilityProblem.xaml


xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:local="clr-namespace:CPALMSStandardsViewer;assembly=CPALMSStandardsViewer"
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
xmlns:converters="clr-namespace:CPALMSStandardsViewer.Converters"
xmlns:Helpers="clr-namespace:CPALMSStandardsViewer.Helper"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="CPALMSStandardsViewer.Views.testVisibilityProblem">

BackgroundColor="White"
SeparatorColor="Gray"
ItemsSource="{Binding CustomEntities}"
VerticalOptions="Fill"
SelectionMode="None"
HasUnevenRows="True">

Command="{Binding SelectCustomEntity}"
EventArgsConverter="{converters:ItemTappedEventArgsConverter}" />













testVisibilityProblemViewModel.cs

using CPALMSStandardsViewer.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CPALMSStandardsViewer.ViewModels
{
public class testVisibilityProblemViewModel : ViewModelBase
{
public ObservableCollection CustomEntities { get; set; }

    public testVisibilityProblemViewModel(INavigationService navigationService)
        : base(navigationService)
    {
        Title = "Test Visibility Problem!";
        CustomEntities = new ObservableCollection<CustomEntity>();
        for (int i = 0; i < 50; i++)
        {
            CustomEntities.Add(new CustomEntity() { Name = "Item " + i, IsSelected = false });

        }
    }

    private DelegateCommand<CustomEntity> _SelectCustomEntity;
    public DelegateCommand<CustomEntity> SelectCustomEntity => _SelectCustomEntity ?? (_SelectCustomEntity = new DelegateCommand<CustomEntity>(ExecuteSelectCustomEntityCommand));

    private void ExecuteSelectCustomEntityCommand(CustomEntity paramData)
    {
        paramData.IsSelected = !paramData.IsSelected;
    }


}

}

CustomEntity.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace CPALMSStandardsViewer.Models
{
public class CustomEntity : INotifyPropertyChanged
{

    public virtual string Name { get; set; }

    private bool _IsSelected;
    public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnPropertyChanged("IsSelected"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string name = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

}

I searched a lot concerning this problem and all that I found is something related to the OnPropertyChanged(), someone misses use the observable collection, other one misses the use of OnPropertyChanged(), other one used it in wrong way...

But my problem is different, it's not about binding because the top items worked as expected and also as I already mentioned the first label becomes hidden which is expected, my problem is that when you turn the IsVisible from false to true for an item that was not rendered on the screen, it simply not working.

I may know the problem but I tried a lot to get it solved but none of the solutions I tried work for me.

Please note that UWP worked as expected, my problem is on the Android platform.

Please HELP!

I this issue fixed??

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/xamarin/Xamarin.Forms/issues/6683?email_source=notifications&email_token=ADEXPD7LRUMFI57C4RMRVMLP54CODA5CNFSM4H326I22YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZI535A#issuecomment-508681716, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADEXPD5NBQ6KCFK3BV4FB23P54CODANCNFSM4H326I2Q.

This issue is happening to me as well.

Same here. It is related to HasUnevenRows and RowHeight. If HasUnevenRows is set to false, works fine for me.

Same for me, is there someone found a fix ?

Please help, if anyone found a solution. I am still facing the same issue with latest version of xamarin.forms

I have reviewed the attached file and created a sample.
Seems to be related to: #6721

Any news about this issue? Does anyone know the last version without this bug?

I have reviewed the attached file and created a sample.
Seems to be related to: #6721

For me its unrelated to this.

Didn't found an update with the issue resolved

I have reviewed the attached file and created a sample.
Seems to be related to: #6721

Updating to Xamarin 4.1.0 solved this for me.

Im currently facing this issue on Xamarin.Forms 4.1.0.581479

Im currently facing the same issue on Xamarin.Forms 4.1.0.709244 on the WPF platform.

Just updated to XF 4.2 and it seems to fix the issue.

Just updated to XF 4.2 and it seems to fix the issue.

Can confirm, updating Xamarin.Forms to 4.2 fixed the issue.

Closing this based on user comments

I'm having the same problem, Please help, if anyone found a solution
I using XF 3.5, I tried to upgrade XF 4.2 and latest version of xamarin.forms but it's not working .

Was this page helpful?
0 / 5 - 0 ratings