我知道这个问题已经有一些很好的答案,但我只想提出一个想法,我认为它实施起来有点困难,但更容易使用。
该解决方案需要来自ListView
's ItemContainerStyleSelector
and a Behavior
from 行为 SDK (XAML).
基本上,这个AlternatingColorItemContainerStyleSelector
我创建的行为允许您指定两个SolidColorBrush
颜色。它封装了创建的逻辑ItemContainerStyleSelector
与两个不同的Style
s 以及分配相应的SolidColorBrush
每一个Style
.
一旦你的行为到位,使用它就非常简单 - 我只需将它拖放到ListView
在 Expression Blend 中指定两种颜色,就这样!
![enter image description here](https://i.stack.imgur.com/8XnAN.png)
这是行为。
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()
{
}
}
}
需要注意的一件事是,删除项目不会更新所有其他项目的颜色(仅仅是因为SelectStyleCore
其他项目的不会被调用),添加项目会。但就你而言,这应该足够了。