How Can i change the default background color of selected listview item.?

Tried this one but did not work .
private void ViewCell_Tapped(object sender, EventArgs e)
{
var viewcell = (ViewCell)sender;
if (viewcell != null) {
viewcell.View.BackgroundColor = Color.Blue;
}

    }

Answers

  • SmartManSmartMan INMember ✭✭✭
    edited October 2018

    Please go through this

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-cell-appearance/

    https://developer.xamarin.com/samples/xamarin-forms/WorkingWithListview/

    Try the below code

    [assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellTransparent))]
    namespace MyApp.iOS.Renderers
    {
    public class ViewCellTransparent : ViewCellRenderer
    {
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
    var cell = base.GetCell(item, reusableCell, tv);
    if (cell != null)
    {
    // Disable native cell selection color style - set as Transparent
    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
    }
    return cell;
    }
    }
    }

  • jezhjezh Member, Xamarin Team Xamurai

    We could use custom renderer to change that property on each platform. Firstly, create your own view cell class:

      public class MyViewCell: ViewCell  
      {
      }
    

    For iOS

       [assembly: ExportRenderer(typeof(MyViewCell), typeof(ViewCellRendererForiOS))]
       namespace ChangeItmColorApp.iOS
     {
           class ViewCellRendererForiOS: ViewCellRenderer
        {
              public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
             {
            var cell = base.GetCell(item, reusableCell, tv);
    
            cell.SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Red };
    
            return cell;
           }
        }
     }
    

    For Android

     [assembly: ExportRenderer(typeof(MyViewCell), typeof(ViewCellRendererForAndroid))]
     namespace Demo.Droid
    {
    public class ViewCellRendererForAndroid : ViewCellRenderer
     {
        private Android.Views.View _cellCore;
        private bool _selected = false;
    
        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
        {
            _cellCore = base.GetCellCore(item, convertView, parent, context);
            return _cellCore;
        }
    
        protected override void OnCellPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args)
        {
            base.OnCellPropertyChanged(sender, args);
            if (args.PropertyName == "IsSelected")
            {
                _selected = !_selected;
                var extendedViewCell = sender as ViewCell;
                if (_selected)
                    _cellCore.SetBackgroundColor(Android.Graphics.Color.Red);
                else
                    _cellCore.SetBackgroundColor(Android.Graphics.Color.Transparent);
            }
          }
        }
     }
    

    Moreover if you want to change the color on forms dynamically instead of hard code. Try to define a bindable property in your custom view cell class. Then configure it on each platform.

    Attachment is a whole sample you can refer to.

Sign In or Register to comment.