Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Listview selectedItem background color *HACK*

$
0
0

Here the solution I came up with. It is more of a hack, but like many others I needed a solution to set the background color of the selectedItem in the listview. It works with the limited tested I did. I know it a hack, but did not find any other solutions to this problem.

public class MyBinder
    {
        public MyBinder(string title, string details)
        {
            this.Title = title;
            this.Details= details;
        }

        public string Title {
            get;
            set;
        }

        public string Details {
            get;
            set;
        }
    }

    public class MYListView : ContentPage
    {
        private MyViewCell lastView;
        private Color MyBackgroundColor = Color.Transparent;

        public MYListView ()
        {

            this.Title = "Sample";

            var data = new List<MyBinder> ();
            data.Add (new MyBinder ("X", "Y"));
            data.Add (new MyBinder ("Title", "Details - Y"));
            var list = new ListView {   
                HasUnevenRows = true,
                ItemTemplate = new DataTemplate(typeof(MyViewCell))
            };


            list.ItemTapped += (object sender, ItemTappedEventArgs e) => {
                var item = e.Item as MyBinder;
                var group = e.Group as IList;

                if (MyBackgroundColor == Color.Transparent)
                {
                    MyBackgroundColor = (group[0] as ViewCell).View.BackgroundColor;
                }
                foreach(var itX in group)
                {
                    var t2 = itX as MyViewCell;
                    if (t2.Item == item)
                    {
                        t2.View.BackgroundColor = Color.Yellow;
                    }
                    else
                    {
                        t2.View.BackgroundColor = MyBackgroundColor;
                    }
                }
            };
            list.ItemsSource = data;
            this.Content = new StackLayout {
                Children = {
                    list,
                    new Button { Text = "Button 1" }
                }
            };
        }
    }


public class MyViewCell : ViewCell
{
    public MyBinder Item {
        get;
        set;
    }

    public MyViewCell ()
    {
    }

    protected override void OnBindingContextChanged ()
    {
        base.OnBindingContextChanged ();

        Item = (MyBinder)this.BindingContext;
        var l = new Label ();
        l.SetBinding (Label.TextProperty, new Binding ("Title", BindingMode.TwoWay));
        View = new StackLayout {
            Children = {
                l,
                new Label { Text = Item.Details }
            }
        };
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>