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

前端之家收集整理的这篇文章主要介绍了Windows Phone 8.1中ListView中行的交替颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一个 Windows Phone 8.1运行时应用程序.

我正在使用ListView控件.

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

搜索后我发现这个链接a previous answer.

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

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

我的建议是使用带有附加DependencyProperties的Converter类.当您初始化转换器时,您可以定义要引用的项目集合以及背景的备用画笔列表.它可以像这样寻找例子:
  1. public class AlternateConverter : DependencyObject,IValueConverter
  2. {
  3. public List<SolidColorBrush> AlternateBrushes
  4. {
  5. get { return (List<SolidColorBrush>)GetValue(AlternateBrushesProperty); }
  6. set { SetValue(AlternateBrushesProperty,value); }
  7. }
  8.  
  9. public static readonly DependencyProperty AlternateBrushesProperty =
  10. DependencyProperty.Register("AlternateBrushes",typeof(List<SolidColorBrush>),typeof(AlternateConverter),new PropertyMetadata(new List<SolidColorBrush>()));
  11.  
  12. public object CurrentList
  13. {
  14. get { return GetValue(CurrentListProperty); }
  15. set { SetValue(CurrentListProperty,value); }
  16. }
  17.  
  18. public static readonly DependencyProperty CurrentListProperty =
  19. DependencyProperty.Register("CurrentList",typeof(object),new PropertyMetadata(null));
  20.  
  21. public object Convert(object value,Type targetType,object parameter,string language)
  22. { return AlternateBrushes[(CurrentList as IList).IndexOf(value) % AlternateBrushes.Count]; }
  23.  
  24. public object ConvertBack(object value,string language)
  25. { throw new NotImplementedException(); }
  26. }

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

  1. // somewhere in your DataContext
  2. private List<SolidColorBrush> brushes = new List<SolidColorBrush> { new SolidColorBrush(Colors.Red),new SolidColorBrush(Colors.Blue) };
  3. public List<SolidColorBrush> Brushes { get { return brushes; } }

你可以这样使用:

  1. <ListView x:Name="myList" ItemsSource={Binding MyItems}>
  2. <ListView.Resources>
  3. <local:AlternateConverter CurrentList="{Binding ElementName=myList,Path=ItemsSource}"
  4. AlternateBrushes="{Binding Brushes}"
  5. x:Key="AlternateConverter"/>
  6. </ListView.Resources>
  7. <ListView.ItemTemplate>
  8. <DataTemplate>
  9. <Border Background="{Binding Converter={StaticResource AlternateConverter}}">
  10. <!-- your itemtemplate -->
  11. </Border>
  12. </DataTemplate>
  13. </ListView.ItemTemplate>
  14. </ListView>

这个解决方案应该是有效的,虽然它有价值类型的IList可能有问题.这里也不应该是延迟创建的问题,因为它直接从列表中搜索索引.

猜你在找的Windows相关文章