Xamarin Forms CustomCell in Listview Button Background binding not working?

Jesper Højer

I have a shared Xamarin.Forms project, and the problem only exists on Android. My problem is, that I have a listview, and when I click a button in my customcell, it changes color(from blue to green). Then I click another button which opens another page, and when I close that page, the item is removed from the listview. But now the item below the removed one, has a green button, instead of a blue. Here's an example:

  1. First image - Showing the ListView with a customcell inside containing information and 2 buttons, notice that they are blue.

enter image description here

  1. Second image - Showing that I have pressed the first button, and it has now turned green.

enter image description here

  1. Thrid image - Showing the page that is pushed when I press the second button.

enter image description here

  1. Fourth image - Now I have clicked the "Bekræft" button on the image before, and the Message has been sent to the Listview page to remove the RouteElement from the list (and so it does). But now the first button is green, even though it has not been pressed.

enter image description here

The RouteElement Model.

 public class RouteElement : INotifyPropertyChanged
 {
    string arrivalBtnColor;
    public event PropertyChangedEventHandler PropertyChanged;

    public DateTime ArrivalTime { get; set; }
    public DateTime DepartureTime { get; set; }
    public bool ReadyForService { get; set; }
    public bool DeliveredToService { get; set; }
    public string ArrivalBtnBColor
    {
        get { return arrivalBtnColor; }
        set
        {
            if (arrivalBtnColor != value)
            {
                arrivalBtnColor = value;
                OnPropertyChanged("ArrivalBtnBColor");
            }
        }
    }
    public RouteElement()
    {
        this.ArrivalBtnBColor = "Default";
    }
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

The CustomCell

Button ArrivalBtn = new Button
  {
    Text = "Ankomst",
    FontSize = 24,
    BorderRadius = 10,
    HeightRequest = 75,
    TextColor = Color.FromHex("#FFFFFF")
  };
 ArrivalBtn.SetBinding(Button.BackgroundColorProperty, "ArrivalBtnBColor",BindingMode.Default, new StringToColorConverter(), null);

Label PostalNoLbl = new Label()
            {
                TextColor = Color.Black,
                HorizontalTextAlignment = TextAlignment.Center,
                VerticalOptions = LayoutOptions.Start,
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
            };
            PostalNoLbl.SetBinding(Label.TextProperty, "Postcode");
            PostalNoLbl.SetBinding(Label.IsVisibleProperty, "Postcode", BindingMode.Default,new StringToBoolConverter(),null);

Then I call this MessagingCenter function to remove, from another page in the navigation.

MessagingCenter.Subscribe<RouteElement>(this, "Refresh",(sender) =>
{
   RouteElement r = (RouteElement)sender;
   rOC.Remove(r);
}

And now the button of the second RouteElement is green, even though it's supposed to be blue. Any help is much appreciated!

This "bug" happens only on Android with the newest package of Xamarin.Forms

<package id="Xamarin.Forms" version="2.3.3.193" targetFramework="monoandroid70" />

It works fine on Android with this package of Xamarin.Forms

<package id="Xamarin.Forms" version="2.2.0.31" targetFramework="monoandroid70" />
Alex West

Are you defining a ListViewCachingStrategy for your ListView? You could try either:

_listView = new ListView(ListViewCachingStrategy.RecycleElement);

or

_listView = new ListView(ListViewCachingStrategy.RetainElement);

The ListView may be incorrectly reusing the color (but not the text/content) from the old cell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Xamarin Forms Button Command binding inside a ListView

Custom ListView binding not working in Xamarin.Forms

Xamarin Forms Command Binding inside ListView is not working

Xamarin Forms Button IsVisible Binding not Working on Android

Xamarin Forms ListView Binding

Xamarin binding is not working to listview

ListView Data Binding in Xamarin Forms

Binding values not working on Xamarin Forms

How to set Binding Context of ListView button to the binding context of parent in Xamarin Forms

Xamarin Forms ListView Grouping Contents not Binding

ListView Data template binding in Xamarin forms

Sub list is not binding to ListView grouping in xamarin forms

Binding ReactiveList<T> to ListView in Xamarin Forms

Xamarin Forms ListView Data Binding nested objects

Xamarin.Forms ListView Binding Issue

ListView SelectedItem Binding with Xamarin Forms and ReactiveUI Issue

Xamarin Forms - Binding Listview for lazy loading

Data not binding in Xamarin forms Listview group

Xamarin Forms: Binding *Shared Resource* Images to a ListView?

Background color of selecteditem in listview xamarin.forms

Change the Background Color of an ListView Item on Xamarin Forms

Xamarin Forms Binding Label Text not working

Binding Bindable Property Command Not working In Xamarin forms

Binding of image trigger in the GroupHeaderTemplate of Xamarin Forms not working

Binding to the ViewModel not working in Xamarin.Forms

Binding of Button inside a ListView item (Xamarin)

Listview XAML in Xamarin.Forms with button

Handle button in listview Xamarin.Forms

ScrollTo not working with grouped ListView in Xamarin.Forms