如何使列表视图具有交替的背景颜色? Net Maui
我正在尝试创建一个列表视图,背景颜色在列表中的每个条目都交替。有没有办法在毛伊岛这样做?
I am trying to create a list view where the background color alternates for each entry in the list. Is there a way to do this in MAUI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了解决这个问题,我在视图模型中添加了亚麻属属性,并使用了一个值转换器来设置颜色。
值转换器:
注册值转换器
我在我的主资源词典中注册了值转换器,因此
可以使用不同颜色的其他键注册一个以上的值转换器组合。
使用值转换器
,我在XAML中使用了绑定的值转换器,例如:
To solve this problem I added a LineNumber property to my view model and used a value converter to set the color.
The value converter:
Register the value converter
I registered the value converter in my main resource dictionary like so:
You can register more than one value converter with a different key for different colour combinations.
Use the value converter
And I used the bound value converter in my XAML like so:
这可以通过多种方式完成。通常建议的方法是通过a DatateMplatesElector 。
1.创建A
DataTemplatesElector
保存两个模板并根据项目的索引选择它们:2。在XAML中,我们可以定义两个模板,一个具有替代颜色,一个具有正常颜色。
This can be done in multiple ways.And the method often recommended is through a DataTemplateSelector.
1.Create a
DataTemplateSelector
that holds two templates and selects them based on the index of the item:2.In XAML, we can define two templates, one with the alternate color and one with the normal color.
如这个答案我们可以使用 datateMplatesElector 解决您的问题。
替代解决方案,
您可以在模型中使用属性,然后将其直接绑定到行背景。 (在您的情况下网格的背景颜色)
您可以使用触发器通过考虑逻辑来改变颜色。
( Xamarin> Xamarin form >
As in this answer we can used DataTemplateSelector to resolve your problem.
Alternative solutions,
You can use a property in the model and straight a way bind it to the row background. (In your case Grid's Background color)
You can use triggers make the color change by considering a logic.
( Xamarin Forms Triggers )