Listview Xamarin.Forms列表视图中的交替项背景色

Listview Xamarin.Forms列表视图中的交替项背景色,listview,colors,xamarin.forms,alternate,Listview,Colors,Xamarin.forms,Alternate,我正在使用ViewModel并将ListView的ItemSource属性绑定到ViewModel的属性。。 搜索internet以查找交替项目背景色的解决方案,但找不到。您可以使用以下代码: int position = 1; foreach (var i in yourItemList) { youranotherProperty = ""; ItemBackgroundColor = (position % 2 == 0)? "Green" : "Red" ; posit

我正在使用ViewModel并将ListView的ItemSource属性绑定到ViewModel的属性。。
搜索internet以查找交替项目背景色的解决方案,但找不到。

您可以使用以下代码:

int position = 1;
foreach (var i in yourItemList)
{
   youranotherProperty = "";
   ItemBackgroundColor = (position % 2 == 0)? "Green" : "Red" ;
   position++;
}

您可以使用以下代码:

int position = 1;
foreach (var i in yourItemList)
{
   youranotherProperty = "";
   ItemBackgroundColor = (position % 2 == 0)? "Green" : "Red" ;
   position++;
}

这可以通过多种方式实现。我认为更好的方法之一是通过
DataTemplateSelector
。请阅读此处的文档:

创建一个包含两个模板的
DataTemplateSelector
,并根据项目的索引选择它们:

public class AlternateColorDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate EvenTemplate { get; set; }
    public DataTemplate UnevenTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        // TODO: Maybe some more error handling here
        return ((List<string>)((ListView)container).ItemsSource).IndexOf(item as string) % 2 == 0 ? EvenTemplate : UnevenTemplate;
    }
}
public类AlternateColorDataTemplateSelector:DataTemplateSelector
{
公共数据模板事件模板{get;set;}
公共数据模板{get;set;}
受保护的覆盖数据模板OnSelectTemplate(对象项,BindableObject容器)
{
//TODO:这里可能会有更多的错误处理
return((列表)((ListView)container).ItemsSource).IndexOf(项作为字符串)%2==0?EventTemplate:EventTemplate;
}
}
现在,在XAML中可以定义两个模板,一个是交替颜色,另一个是正常颜色。如果您想:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AlternateRowColorSample" x:Class="AlternateRowColorSample.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="evenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="White">
                        <Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="unevenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="LightGray">
                        <Label Text="{Binding .}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
                EvenTemplate="{StaticResource evenTemplate}"
                UnevenTemplate="{StaticResource unevenTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ListView ItemTemplate="{StaticResource alternateColorDataTemplateSelector}" ItemsSource="{Binding Items}">

    </ListView>
</ContentPage>

运行时,您将看到灰色和白色的行,如下所示:


可以在此处找到完整的工作示例:

这可以通过多种方式完成。我认为更好的方法之一是通过
DataTemplateSelector
。请阅读此处的文档:

创建一个包含两个模板的
DataTemplateSelector
,并根据项目的索引选择它们:

public class AlternateColorDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate EvenTemplate { get; set; }
    public DataTemplate UnevenTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        // TODO: Maybe some more error handling here
        return ((List<string>)((ListView)container).ItemsSource).IndexOf(item as string) % 2 == 0 ? EvenTemplate : UnevenTemplate;
    }
}
public类AlternateColorDataTemplateSelector:DataTemplateSelector
{
公共数据模板事件模板{get;set;}
公共数据模板{get;set;}
受保护的覆盖数据模板OnSelectTemplate(对象项,BindableObject容器)
{
//TODO:这里可能会有更多的错误处理
return((列表)((ListView)container).ItemsSource).IndexOf(项作为字符串)%2==0?EventTemplate:EventTemplate;
}
}
现在,在XAML中可以定义两个模板,一个是交替颜色,另一个是正常颜色。如果您想:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AlternateRowColorSample" x:Class="AlternateRowColorSample.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="evenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="White">
                        <Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="unevenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="LightGray">
                        <Label Text="{Binding .}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
                EvenTemplate="{StaticResource evenTemplate}"
                UnevenTemplate="{StaticResource unevenTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ListView ItemTemplate="{StaticResource alternateColorDataTemplateSelector}" ItemsSource="{Binding Items}">

    </ListView>
</ContentPage>

运行时,您将看到灰色和白色的行,如下所示:


可以在此处找到完整的工作示例:

您可以在视图模型中添加一个属性,如itemBackgroundColor。然后,在添加列表时,如果项目位置为奇数,则可以使用颜色1,否则使用颜色2。然后像
BackgroundColor={Binding itemBackgroundColor}
一样绑定xaml。从哪里可以获得项目索引?可以在视图模型中添加一个属性,如itemBackgroundColor。然后,在添加列表时,如果项目位置为奇数,则可以使用颜色1,否则使用颜色2。然后像
BackgroundColor={Binding itemBackgroundColor}
一样绑定xaml。从哪里可以获得项目的索引?`public ICommand LoadData=>new命令(async()=>{await GetPOList();_SetBackgroundColor();})`<代码>公共void(public void)SetBackgroundColor(){int position=1;foreach(POList中的变量i){BackgroundColor=(position%2==0)?“蓝色”:“红色”;position++;}}public ICommand LoadData//此命令在出现重载GetPOList()时运行; //这个方法从远程api获取列表_SetBackgroundColor()我在GetPOList()之后调用这个方法;此方法完成..您只需在viewmodel中添加一个额外属性,并且在LoadData()方法中获取数据时绑定此属性。未获取:(为什么需要额外属性以及如何使用此属性?`public ICommand LoadData=>new命令(async()=>{wait GetPOList();_SetBackgroundColor();})“`code>public void\u SetBackgroundColor(){int position=1;foreach(POList中的vari){BackgroundColor=(position%2==0)?“Blue”:“Red”;position++;}}public ICommand LoadData//此命令在出现重载GetPOList时运行();//此方法从远程api获取列表_SetBackgroundColor()我在GetPOList()之后立即调用此方法;此方法完成..您只需在viewmodel中添加一个额外属性,并且在LoadData()方法中获取数据时绑定此属性。未获取:(为什么我需要额外的财产以及如何使用该财产?