在交替的ListView行的颜色在的Windows Phone 8.1 [英] Alternating Colors of rows in ListView in Windows Phone 8.1

查看:130
本文介绍了在交替的ListView行的颜色在的Windows Phone 8.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows Phone的8.1运行时应用。

I have created a Windows Phone 8.1 run-time app.

我使用的ListView控件。

I am using the ListView control.

我要交替各行的背景颜色。

I want to alternate the each background row color.

搜索后,我发现这个链接一个previous答案

After searching I found this link a previous answer.

但是,这给出了标记错误。一方面没有'AlternationCount'属性。我假定这是因为它不是SilverLight的,但RT?

But this gives errors in the markup. For one thing there is no 'AlternationCount' property. I am assuming this is because it is not SilverLight but RT?

如果任何人都可以给我一个链接,因为我struggerling找到一个简单的例子。更好的一个简单的code例子是AP preciated。

If anyone can send me a link as I am struggerling to find a simple example. even better a simple code example would be appreciated.

推荐答案

我的建议是使用的转换的类额外的 DependencyProperties 的。当你intialize转换器,您可以定义它会参考哪些项目收集和备用刷背景的列表。它可以看看例如是这样的:

My proposal is to use a Converter class with additional DependencyProperties. When you intialize the converter, you define to which items collection it will refer and a list of alternate brushes for a background. It can look for example like this:

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(); }
}

一旦你拥有了它定义和创建替代画笔列表:

Once you have it defined and create list of alternate brushes:

// 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; } }

您可以使用它是这样的:

You can use it like this:

<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>

这个解决方案应该工作,但是当你有值类型的IList它可能有问题。而且,这里不应该与递延创作的问题,因为它直接从列表retrives的索引。

This solution should work, though it may have problem when you have IList of value types. Also here shouldn't be problems with deferred creation, as it retrives the index directly from list.

这篇关于在交替的ListView行的颜色在的Windows Phone 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆