Windows Phone 8.1中ListView中行的交替颜色

And*_*son 11 listview listviewitem windows-runtime windows-phone-8.1

我创建了一个Windows Phone 8.1运行时应用程序.

我正在使用ListView控件.

我想交替每个背景行颜色.

搜索后我发现此链接是之前的答案.

但这会给标记带来错误.一方面,没有'AlternationCount'属性.我假设这是因为它不是SilverLight而是RT?

如果有人可以给我发送链接,因为我正在寻找一个简单的例子.更好的是一个简单的代码示例将不胜感激.

Jus*_* XL 6

我知道这个问题已经有了一些很好的答案,但我只是想提出一个更多的想法,我认为它实施起来有点难,但更容易使用.

该解决方案将需要从帮助ListViewItemContainerStyleSelectorBehavior行为SDK(XAML) .

基本上,AlternatingColorItemContainerStyleSelector我创建的这种行为允许您指定两种SolidColorBrush颜色.它封装了创建ItemContainerStyleSelector具有两个不同Styles 的逻辑以及为SolidColorBrush每个s分配相应的逻辑Style.

一旦你有了行为,使用它非常简单 - 我只需要将它拖放到ListViewExpression Blend中并指定两种颜色即可!

在此输入图像描述

这是行为.

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()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

需要注意的一件事是删除项目不会更新所有其他项目的颜色(仅仅因为SelectStyleCore其他项目不会被调用),添加项目将会.但在你的情况下,这应该足够了.

  • 嗨,我从来不知道我会得到这个特殊问题的高质量答案.谢谢! (2认同)
  • 非常好的一个和工作解决方案.大! (2认同)

Rom*_*asz 5

我的建议是使用带有其他DependencyPropertiesConverter类.初始化转换器时,您可以定义它将引用的项集合以及背景的备用画笔列表.它可以看起来像这样的例子:

public class AlternateConverter : DependencyObject, IValueConverter
{
    public List<SolidColorBrush> AlternateBrushes
    {
        get { return (List<SolidColorBrush>)GetValue(AlternateBrushesProperty); }
        set { SetValue(AlternateBrushesProperty, value); }
    }

    public static readonly DependencyProperty AlternateBrushesProperty =
        DependencyProperty.Register("AlternateBrushes", typeof(List<SolidColorBrush>), 
        typeof(AlternateConverter), new PropertyMetadata(new List<SolidColorBrush>()));

    public object CurrentList
    {
        get { return GetValue(CurrentListProperty); }
        set { SetValue(CurrentListProperty, value); }
    }

    public static readonly DependencyProperty CurrentListProperty =
        DependencyProperty.Register("CurrentList", typeof(object),
        typeof(AlternateConverter), new PropertyMetadata(null));

    public object Convert(object value, Type targetType, object parameter, string language)
    { return AlternateBrushes[(CurrentList as IList).IndexOf(value) % AlternateBrushes.Count]; }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    { throw new NotImplementedException(); }
}
Run Code Online (Sandbox Code Playgroud)

一旦定义了它并创建备用画笔列表:

// somewhere in your DataContext
private List<SolidColorBrush> brushes = new List<SolidColorBrush> { new SolidColorBrush(Colors.Red), new SolidColorBrush(Colors.Blue) };
public List<SolidColorBrush> Brushes { get { return brushes; } }
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

<ListView x:Name="myList" ItemsSource={Binding MyItems}>
  <ListView.Resources>
    <local:AlternateConverter CurrentList="{Binding ElementName=myList, Path=ItemsSource}" 
                                      AlternateBrushes="{Binding Brushes}"
                                      x:Key="AlternateConverter"/>
  </ListView.Resources>
  <ListView.ItemTemplate>
     <DataTemplate>
       <Border Background="{Binding Converter={StaticResource AlternateConverter}}">
          <!-- your itemtemplate -->
       </Border>
     </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

这个解决方案应该可行,但是如果你有IList的值类型可能会有问题.此处也不应该是延迟创建的问题,因为它直接从列表中检索索引.