[英]How to change the background color of `TextCell` inside `ListView` in Xamarin Forms?
I have a ListView
set up like below: 我有一个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>
I have a renderer for iOS and set the following: 我有一个iOS渲染器,并设置了以下内容:
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.BackgroundColor = UIColor.Red;
return cell;
}
Somehow my whole ListView
is using the Gray
background I set for the listview. 不知何故,我的整个ListView
使用的是我为Listview设置的Gray
背景。 What I want to happen is have a Gray
color background for the ListView
(which means the header part would have a gray background) and have Red
color background for the TextCell
. 我想发生的是ListView
具有Gray
背景(这意味着标题部分将具有灰色背景),而TextCell
具有Red
背景。 Am I setting the wrong property to get the desired background for TextCell
? 我是否设置了错误的属性来获取TextCell
所需的背景?
Your custom renderer is not actually returning the cell, so the change you make to the BackgroundColor
is not coming through. 您的自定义渲染器实际上并未返回该单元格,因此您对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;
}
Update: You should be setting cell.ContentView.BackgroundColor
instead. 更新:您应该改为设置cell.ContentView.BackgroundColor
。
Add your textcell inside Grid/StackLayout and assign BackgroundColor="Red"
for corresponding layout directly in the xaml. 将您的文本单元添加到Grid / StackLayout内,然后直接在xaml中为相应的布局分配BackgroundColor="Red"
。 Your don't need to write custom renderer for that. 您无需为此编写自定义渲染器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.