Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

CustomCell in Listview Button Background binding not working?

JeeepzJeeepz USMember ✭✭

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. Kind of hard to explain, but here is some code.

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);

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!

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Sounds like standard selection from a ListView. That's what ListView does: It lets you select something from a list of choices, and highlights the .SelectedItem. I suspect you're selecting the item when you click the button in the listview item.

    If you don't care about the selected item and only care about the button click, then you could set the selection of the listview back to null in the click handler.

    Kind of hard to explain,

    If I'm misreading what's happening screen shots might help a lot.

  • JeeepzJeeepz USMember ✭✭
    edited February 2017

    Alright, you misunderstood me, @ClintStLaurent . I'll try with images.
    This is what happens.

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

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

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

    4. 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.

    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" />

  • EsamSherifEsamSherif EGUniversity ✭✭

    When an item from ListView.ItemsSource is removed, the cell itself is not removed... instead the cell is reused and bound to another Item from ItemsSource, This behavior changes the bindings only but anything set in xaml stays the same.

    Possible Solution:
    Create a bindable property for highlighting the button too.
    (i didnt really try it, but it will need more logic to achieve when exactly should the button be highlighted )

  • JeeepzJeeepz USMember ✭✭

    Possible Solution:
    Create a bindable property for highlighting the button too.
    (i didnt really try it, but it will need more logic to achieve when exactly should the button be highlighted )

    If you read the first comment, under "The CustomCell", you'll see that I am binding the backgroundcolor of the button already.. So it is not a working solution.

Sign In or Register to comment.