Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternating Colors of rows in ListView in Windows Phone 8.1

I have created a Windows Phone 8.1 run-time app.

I am using the ListView control.

I want to alternate the each background row color.

After searching I found this link a previous answer.

But this gives errors in the markup. For one thing there is no 'AlternationCount' property. I am assuming this is because it is not SilverLight but RT?

If anyone can send me a link as I am struggerling to find a simple example. even better a simple code example would be appreciated.

like image 211
Andrew Simpson Avatar asked Dec 22 '14 17:12

Andrew Simpson


1 Answers

I know there's already a few good answers to this question but I just want to throw in one more idea which I think it's a bit harder to implement but easier to use.

This solution will need the help from the ListView's ItemContainerStyleSelector and a Behavior from Behavior SDK (XAML).

Basically, this AlternatingColorItemContainerStyleSelector behavior I've created allows you to specify two SolidColorBrush colors. It encapsulates the logic of creating a ItemContainerStyleSelector with two different Styles as well as assigning the corresponding SolidColorBrush to each Style.

Once you have the behavior in place, to use it is extremely simple - I only needed to drag and drop it onto the ListView in Expression Blend and specify two colors and that's it!

enter image description here

Here's the behavior.

namespace Behaviors
{
    public class AlternatingColorItemContainerStyleSelector : StyleSelector
    {
        private Style _oddStyle = new Style { TargetType = typeof(ListViewItem) }, _evenStyle = new Style { TargetType = typeof(ListViewItem) };
        public Style OddStyle { get { return _oddStyle; } }
        public Style EvenStyle { get { return _evenStyle; } }

        protected override Style SelectStyleCore(object item, DependencyObject container)
        {
            var listViewItem = (ListViewItem)container;
            var listView = GetParent<ListView>(listViewItem);

            var index = listView.IndexFromContainer(listViewItem);

            if (index % 2 == 0)
            {
                return this.EvenStyle;
            }
            else
            {
                return this.OddStyle;
            }
        }

        public static T GetParent<T>(DependencyObject child) where T : DependencyObject
        {
            while (!(child is T))
            {
                child = VisualTreeHelper.GetParent(child);
            }

            return (T)child;
        }
    }

    public class ListViewAlternatingColorBehavior : DependencyObject, IBehavior
    {
        public DependencyObject AssociatedObject { get; set; }

        public Style SharedItemContainerStyle { get; set; }

        #region colors dp

        public SolidColorBrush OddBrush
        {
            get { return (SolidColorBrush)GetValue(OddBrushProperty); }
            set { SetValue(OddBrushProperty, value); }
        }

        public static readonly DependencyProperty OddBrushProperty =
            DependencyProperty.Register("OddBrush", typeof(SolidColorBrush), typeof(ListViewAlternatingColorBehavior), new PropertyMetadata(null));

        public SolidColorBrush EvenBrush
        {
            get { return (SolidColorBrush)GetValue(EvenBrushProperty); }
            set { SetValue(EvenBrushProperty, value); }
        }

        public static readonly DependencyProperty EvenBrushProperty =
            DependencyProperty.Register("EvenBrush", typeof(SolidColorBrush), typeof(ListViewAlternatingColorBehavior), new PropertyMetadata(null));

        #endregion

        public void Attach(DependencyObject associatedObject)
        {
            this.AssociatedObject = associatedObject;

            this.ApplyItemContainerStyleSelectors();
        }

        private void ApplyItemContainerStyleSelectors()
        {
            var itemContainerStyleSelector = new AlternatingColorItemContainerStyleSelector();

            if (this.SharedItemContainerStyle != null)
            {
                itemContainerStyleSelector.OddStyle.BasedOn = this.SharedItemContainerStyle;
                itemContainerStyleSelector.EvenStyle.BasedOn = this.SharedItemContainerStyle;
            }

            itemContainerStyleSelector.OddStyle.Setters.Add(new Setter { Property = Control.BackgroundProperty, Value = this.OddBrush });
            itemContainerStyleSelector.EvenStyle.Setters.Add(new Setter { Property = Control.BackgroundProperty, Value = this.EvenBrush });

            var listView = (ListView)this.AssociatedObject;
            listView.ItemContainerStyleSelector = itemContainerStyleSelector;
        }

        public void Detach()
        {
        }
    }
}

One thing to note is that removing items won't update all the other items' colors (simply because SelectStyleCore of other items won't be called), adding items will. But in your case this should be enough.

like image 186
Justin XL Avatar answered Nov 28 '22 07:11

Justin XL