替代行颜色ListView xamarin形式 [英] Alternate row color ListView xamarin forms

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

问题描述

我已经绑定到我的ListView(一个ObersvableCollection)上.

I have bind on my ListView, an ObersvableCollection.

我想替换我的ListView的行颜色.

And i want to alternate my row color of my ListView.

我找到了很多代码,但对我却没用...如果您可以共享一个示例/示例!

I found lot of code, but didn't work for me... If you can share an example/sample !

喜欢这个:

但是我不知道该怎么办?

But i don't know how i can do that ?

我使用Visual Studio 2015/Xamarin表单.

I work with Visual Studio 2015 / Xamarin forms.

我的项目必须与Android和IOS兼容.

My project must be work with Android and IOS.

感谢您的帮助!

推荐答案

没有使用XF进行此操作的内置方法.最简单的方法是在项目模型中包括一个Index属性(确定将属性添加到List/Collection时必须手动设置),该属性可以确定颜色.然后,您可以将RowColor属性绑定到ItemTemplate.

There's no built in way to do this with XF. The simplest approach would be to include an Index property in your Item model (you would have to set it manually as you add it to your List/Collection) that determines the color. You could then bind the RowColor property to your ItemTemplate.

public class MyItem {

  public int Index { get; set; }
  public string Name { get; set; }

  public Color RowColor { 
    get {
      if (Index % 2) == 0))
        return Color.Red;
      else
        return Color.Blue; 
    }
  }
}

您还可以使用ValueConverter根据索引"行确定颜色-这将使您的模型不必确定其自己的颜色,这将是一种更清洁的实现.

You could also use a ValueConverter to determine the Color based on the Index row - this would free your model from having to determine it's own color, which would be a cleaner implementation.

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

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