jdmdevdotnet
jdmdevdotnet

Reputation: 1

Xamarin Forms Listview disable cell highlight

This seems like a really primitive problem and should be solved with a property changed. But here's my issue:

One of the only ways around this that I've seen is NOT setting the SelectedItem to null. Which I do because if I don't, and I go back to my listview, I can't select the same item.

You'd think that Xamarin has a property on the listview HighlightCellSelection and I can just set it to false. Anywho, any way around this?

Here's my code:

ListView.ItemSelected += (s, e) =>
                {
                    ((ListView)s).BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Black : Xamarin.Forms.Color.White;
                    XamarinMobile.ViewModels.GridCellViewModel cell = (XamarinMobile.ViewModels.GridCellViewModel)e.SelectedItem;
                    ((ListView)s).SelectedItem = null;
                }

Also in my binding context, which does the same thing:

    this.Tapped += (s, e) => {
        this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Black : Xamarin.Forms.Color.White;
    };

Upvotes: 0

Views: 544

Answers (2)

jdmdevdotnet
jdmdevdotnet

Reputation: 1

Basically what was happening was that the property didn't get set if I set it to the same as I did before. It was fixed by simply changing it to transparent first and then the desired color.

Upvotes: 0

Nick Peppers
Nick Peppers

Reputation: 3251

I'm assuming this is on iOS, but if I understood your question correctly and all you want to do is disable default Cell highlighting, you can make a ViewCellRenderer and change the cell selection style.

public class ViewCellRenderer : Xamarin.Forms.Platform.iOS.ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        cell.SelectionStyle = UITableViewCellSelectionStyle.None;
        cell.BackgroundColor = UIColor.Clear; 
        return cell;
    }
}

Upvotes: 1

Related Questions