我们知道,在WPF里,MSDN提供了三种方法

1.使用转换器Converter

2.继承ListView类,自己处理

3.使用StyleSelctor

到了WinRT的世界了

1. Winrt中Setter不能Binding,需要找办法解决,正在找办法解决(已经找到解决方法,请参看WinRT ListView间隔变色(二))

2.继承类肯定是可以的。就不尝试了

3.使用ItemContainerStyleSelector

我们从第三种开始做起:

在Winrt中,我们没有办法直接重写SelectStyle构造函数了,好在天无绝人之路。WinRt贴心的提供了SelectStyleCore函数。

 protected virtual Style SelectStyleCore(System.Object item, DependencyObject container);

接下来我们就重写这个函数就好

   protected override Style SelectStyleCore(object item, DependencyObject container)
{
var style = new Style {TargetType = typeof (ListViewItem)};
var backgroudSetter = new Setter {Property = Windows.UI.Xaml.Controls.Control.BackgroundProperty}; var listview = ItemsControl.ItemsControlFromItemContainer(container) as ListView;
if (listview != null)
{
var index = listview.IndexFromContainer(container);
backgroudSetter.Value = index%== ? EvenColorBrush : OddColorBrush ;
}
style.Setters.Add(backgroudSetter); return style;
}

这里,我们定义了连个Brush( EvenColorBrush ,OddColorBrush),方便我们后来修改

public SolidColorBrush OddColorBrush { get; set; }
public SolidColorBrush EvenColorBrush { get; set; }

我们把这个StylesSecltor命名为ListViewItemStyleSelector;在资源文件里使用

 <converter:ListViewItemStyleSelector x:Key="ListViewItemStyleSelector" OddColorBrush="{StaticResource SilverColorBrush}" EvenColorBrush="{StaticResource AsbestosColorBrush}"/>

其中,converter前缀为ListViewItemStyleSelector所在的命名空间。

最后,我们在项目中使用这个Selector,

 <ListView ItemsSource="{Binding VM.AuthorPostList}"  ItemContainerStyleSelector="{StaticResource  ListViewItemStyleSelector}"  ItemTemplate="{StaticResource AuthorPostDetailItemDataTemplate}" Grid.Row=""/>

我们最终效果如下:

接下来,还有第二篇有关间隔变色的问题 WinRT ListView间隔变色(二)


写完这个,经高手提醒,还可以用Behavior来做

Alternating Colors of rows in ListView in Windows Phone 8.1

摘录主要代码如下:

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 % == )
{
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()
{
}
}
}

使用时如下:

<interactivity:Interaction.Behaviors>
behaviors:ListViewAlternatingColorBehavior EvenBrush="Red" OddBrush="Green"/>
</interactivity:Interaction.Behaviors>

WinRT ListView间隔变色(一)的更多相关文章

  1. WinRT ListView间隔变色(二)

    上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...

  2. wpf listview 行变色

    <ListView x:Name="listView_Date" Grid.Row="3" BorderBrush="{x:Null}" ...

  3. ListView间隔设置颜色

    在Adapter的getView中 if (position % 2 == 0) holder.shoucangbt.setBackgroundColor( Color.rgb(246, 246, 2 ...

  4. ListView的基本使用技巧

    ListView的基本使用技巧 1.headerView和footerView 2.ViewHolder 3.OnScrollListener 4.单行刷新 5.其它细节 ListView提供head ...

  5. GridView

    1.设置间隔变色 css: .oddRow{ background:#fafafa; } js: ,loadComplete:function(){ $("tr.jqgrow:odd&quo ...

  6. Android开发颜色大全

    <!-- dialog背景颜色 --> <color name=</color> <color name="white">#FFFFFF& ...

  7. Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色

    今天作一个项目,就是做视频app,如果电视剧的话有许多剧集,点击一个item,播放不同的剧集,要有点击效果,并且默认是选择第一个.花费了一段时间,自己觉得有点难 度,现在和大家分享一下,下面是效果显示 ...

  8. ListView防止滑动变色的小技巧

    listview滑动时会变成白色,如果背景色不是白色的话可以通过设置setCacheColorHint(Color.TRANSPARENT);来避免变色,.对应的xml也可以进行设置.

  9. 设置ListView每条数据之间的间隔

    1:如果不需要分割线可以在xml布局文件中ListView下设置XML属性: android:divider="#00000000" android:dividerHeight=& ...

随机推荐

  1. 使用 BenchmarkDotnet 测试代码性能 【Win10】单元测试中捕获异步方法的指定异常

    先来点题外话,清明节前把工作辞了(去 tm 的垃圾团队,各种拉帮结派.勾心斗角).这次找工作就得慢慢找了,不能急了,希望能找到个好团队,好岗位吧.顺便这段时间也算是比较闲,也能学习一下和填掉手上的坑. ...

  2. java7新特性之Try-with-resources (TWR)

    java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...

  3. 使用带粒子效果的 CAEmitterLayer

    1.用CAEmitterLayer产生粒子效果 2.封装CAEmitterLayer 3.封装下雪.下雨的粒子效果控件 一.用CAEmitterLayer产生粒子效果 - (void)emitterL ...

  4. [Erlang危机](5.1.4)端口port

      原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 或port drivers15.   全程跟踪端口数会对诊断负载或进程泄漏 ...

  5. Linux地址ping不通情况怎么办?

    查看原文:http://www.ibloger.net/article/325.html Linux地址ping不通情况怎么办? 问题:今天写了一个微信支付的项目.有一个class中使用了httpPo ...

  6. CPU卡的读写【转】

    本文转载自:http://blog.csdn.net/logaa/article/details/7465226 一般来说,对存储卡和逻辑加密卡操作,使用接触式IC卡通用读写器:对CPU卡使用CPU卡 ...

  7. MAMP/xampp安装redis

    nmp/amp/xampp安装redis 一.安装redis服务 1.通过homebrew安装redis sudo brew install redis 2.启动redis服务,且接受客户端连接 su ...

  8. Eclipse 工程配置与目录结构及各种文件夹(常用插件)

    .classpath..project 是 Eclipse 工程所必须的文件. OpenExplorer: 该 jar 包的下载地址:samsonw/OpenExplorer 安装配置方法:eclip ...

  9. [转] 本地项目上传github (新项目 / 旧项目)

    前置:安装Git Bash,在github上新建仓库repository 1.右键点击项目所在文件夹,运行: git bash here.在git bash窗口运行命令 git init 把这个目录变 ...

  10. STM32 的堆栈静态区

    STM32的分区从0x2000 0000开始.静态区,堆,栈. 所有的全局变量,包括静态变量之类的,全部存储在静态存储区.紧跟静态存储区之后的,是堆区(如没用到malloc,则没有该区),之后是栈区, ...