WinRT ListView间隔变色(一)
我们知道,在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间隔变色(一)的更多相关文章
- WinRT ListView间隔变色(二)
上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...
- wpf listview 行变色
<ListView x:Name="listView_Date" Grid.Row="3" BorderBrush="{x:Null}" ...
- ListView间隔设置颜色
在Adapter的getView中 if (position % 2 == 0) holder.shoucangbt.setBackgroundColor( Color.rgb(246, 246, 2 ...
- ListView的基本使用技巧
ListView的基本使用技巧 1.headerView和footerView 2.ViewHolder 3.OnScrollListener 4.单行刷新 5.其它细节 ListView提供head ...
- GridView
1.设置间隔变色 css: .oddRow{ background:#fafafa; } js: ,loadComplete:function(){ $("tr.jqgrow:odd&quo ...
- Android开发颜色大全
<!-- dialog背景颜色 --> <color name=</color> <color name="white">#FFFFFF& ...
- Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色
今天作一个项目,就是做视频app,如果电视剧的话有许多剧集,点击一个item,播放不同的剧集,要有点击效果,并且默认是选择第一个.花费了一段时间,自己觉得有点难 度,现在和大家分享一下,下面是效果显示 ...
- ListView防止滑动变色的小技巧
listview滑动时会变成白色,如果背景色不是白色的话可以通过设置setCacheColorHint(Color.TRANSPARENT);来避免变色,.对应的xml也可以进行设置.
- 设置ListView每条数据之间的间隔
1:如果不需要分割线可以在xml布局文件中ListView下设置XML属性: android:divider="#00000000" android:dividerHeight=& ...
随机推荐
- Android--Activity在跳转时携带数据
首先看看两种传递方法演示样例:(一个简单姻缘计算器) 主Activity import android.os.Bundle; import android.app.Activity; import a ...
- jquery验证插件validate自定义扩展
<script src="${pageContext.request.contextPath}/resources/js/jquery-1.12.0.min.js" type ...
- GMT和CST的转换
GMT时间是格林尼治标准时间.CST时间是指包含中国.美国.巴西,澳大利亚四个时区的时间. 在javascript中默认CST是指美国中部时间,倘若在javascript中GMT转换CST则两者相差1 ...
- mac 查看python路径
1,terminal : input: which python 2, terminal: input : python --->import sys ----> print sys ...
- Oracle新建数据库
确定楼主是以管理员身份登录的:1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password ...
- FFmpeg将多张图片合成视频 子视频合并
FFmpeg将多张图片合成视频 - CSDN博客 https://blog.csdn.net/wangshuainan/article/details/77914508 Slideshow – FFm ...
- 蓝桥 PREV-30 历届试题 波动数列 【动态规划】
历届试题 波动数列 时间限制:1.0s 内存限制:256.0MB 问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. ...
- JMeter快捷键图标制作 去掉cmd命令窗口
使用jmeter时: 如果使用默认的jmeter.bat启动的话,会出现一个CMD命令窗口之后再会启动jmeter工作界面 直接启用ApacheJMeter.jar文件即可跳过CMD命令窗口启动jme ...
- ios19---xib
// // ViewController.m #import "ViewController.h" @interface ViewController () @end @imple ...
- LightTable的结构(二)
这节主要研究下object的一个属性,behaviors 定义一个behavior需要提供name,trigger,reaction (behavior ::on-close-destroy :tri ...