首页 > Windows开发 > 详细

WinRT ListView间隔变色

时间:2015-10-09 11:41:21      阅读:317      评论:0      收藏:0      [点我收藏+]

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

1.使用转换器Converter

2.继承ListView类,自己处理

3.使用StyleSelctor

到了WinRT的世界了

1. Winrt中Setter不能Binding,需要找办法解决,正在找办法解决

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

3.使用ItemContainerStyleSelector

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

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

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

 1   protected override Style SelectStyleCore(object item, DependencyObject container)
 2         {
 3             var style = new Style {TargetType = typeof (ListViewItem)};
 4             var backgroudSetter = new Setter {Property = Windows.UI.Xaml.Controls.Control.BackgroundProperty};
 5 
 6             var listview = ItemsControl.ItemsControlFromItemContainer(container) as ListView;
 7             if (listview != null)
 8             {
 9                 var index = listview.IndexFromContainer(container);
10                 backgroudSetter.Value = index%2==0 ? EvenColorBrush : OddColorBrush ;
11             }
12             style.Setters.Add(backgroudSetter);
13 
14             return style;
15         }

这里,我们定义了连个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="1"/>

 

WinRT ListView间隔变色

原文:http://www.cnblogs.com/startewho/p/4863090.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!