地铁的应用程序 - ListView的 - 如何交替ListViewItems的背景颜色 [英] Metro app - ListView - how to alternate background colour of ListViewItems

查看:140
本文介绍了地铁的应用程序 - ListView的 - 如何交替ListViewItems的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为Windows 8我的Metro风格的应用程序,我绑定列表视图,以一个ObservableCollection,我想每一个ListViewItem的背景颜色交替(白色,灰色,白色等)

In my Metro style app for Windows 8, I'm binding a Listview to an ObservableCollection and I would like the background color of each ListViewItem to alternate (white, gray, white, etc)

   <ListView x:Name="stopsListView" ItemsSource="{Binding}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Height="66" >
                    <TextBlock Text="{Binding Title}" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

在WPF中,这是使用带有触发方式进行的 - 看到的这个页面

In WPF, this is done using a Style with Triggers - see this page.

你怎么做到这一点的地铁应用程序?

How do you accomplish this in a Metro app?

更新:

在正确答案是如下,我就走了,实际上codeD吧。下面是一些code的人谁需要它:

After the correct answer was given below, I went away and actually coded it. Here's some code for anyone who needs it:

code的值转换器类:

Code for value converter class:

public class AltBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is int)) return null;
        int index = (int)value;

        if (index % 2 == 0)
            return Colors.White;
        else
            return Colors.LightGray;
    }

    // No need to implement converting back on a one-way binding
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

code为XAML列表视图:                                 

Code for XAML listview:

    <ListView x:Name="stopsListView" ItemsSource="{Binding}">

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="250" Height="66" Margin="5">
                    <Grid.Background>
                        <SolidColorBrush Color="{Binding IndexWithinParentCollection, Mode=OneWay, Converter={StaticResource AltBGConverter}}" />
                    </Grid.Background>

...而且,当添加物品的收集,或修改集合,记得要在集合中设置其索引:

...and, when adding items to the collection, or modifying the collection, remember to set their Index within the collection:

myCollection.add(item);
item.IndexWithinParentCollection = myCollection.Count;

当然,如果您的收藏经常改变这种做法将是昂贵的维护,因为你将不得不重新索引您的项目,所以我发现它更容易储存的引用,每一个项目中的父集合,然后计算索引上即时使用.IndexOf(),以避免不必不断地每次更新索引值的集合的改变。

Of course, if your collection changes often this approach will be costly to maintain, since you'll have to re-index your items, so I found it easier to store a reference to the parent collection within each item, then calculate the Index on-the-fly using .IndexOf() to avoid having to constantly update the index values every time the collection changes.

推荐答案

您可以使用转换器 - 从项目中抢行索引,并将其转换为一刷。此外 - 如果ItemTemplate中没有给你足够的控制 - 使用ItemContainerStyle修改画笔在ListViewItem的模板级别

You can use a converter - grab a row index from an item and convert it to a brush. Also - if ItemTemplate does not give you enough control - use ItemContainerStyle to modify the brush at the ListViewItem template level.

另一种选择可能是指定一个ItemTemplateSelector,给你一个不同的模板,不同的画笔取决于一个项目。你还需要产生行索引虽然或以某种方式使选择器,以确定该项目是在一个偶或奇位

Another option might be to specify an ItemTemplateSelector that gives you a different template with a different brush depending on an item. You would still need to generate row indices though or somehow enable the selector to determine if the item is at an even or odd position.

这篇关于地铁的应用程序 - ListView的 - 如何交替ListViewItems的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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