![](/img/trans.png)
[英]Change the Background Color of an ListView Item on Xamarin Forms
[英]How to change the background color of `TextCell` inside `ListView` in Xamarin Forms?
我有一个ListView
设置如下:
<ListView HasUnevenRows="true" BackgroundColor="Gray">
<ListView.Header>
<StackLayout Padding="20,35,20,10" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label FontSize="13" TextColor="Gray" Text="Header text here"/>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<ListView.DataTemplate>
<TextCell Text="{Binding CategoryName}" Detail="{Binding Count}">
</ListView.DataTemplate>
</ListView.ItemTemplate>
</ListView>
我有一个iOS渲染器,并设置了以下内容:
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.BackgroundColor = UIColor.Red;
return cell;
}
不知何故,我的整个ListView
使用的是我为Listview设置的Gray
背景。 我想发生的是ListView
具有Gray
背景(这意味着标题部分将具有灰色背景),而TextCell
具有Red
背景。 我是否设置了错误的属性来获取TextCell
所需的背景?
您的自定义渲染器实际上并未返回该单元格,因此您对BackgroundColor
所做的更改不会通过。
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.ContentView.BackgroundColor = UIColor.Red;
return cell;
}
更新:您应该改为设置cell.ContentView.BackgroundColor
。
将您的文本单元添加到Grid / StackLayout内,然后直接在xaml中为相应的布局分配BackgroundColor="Red"
。 您无需为此编写自定义渲染器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.