如何在Xamarin Forms中的ListView内更改TextCell的背景颜色? [英] 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渲染器,并设置了以下内容:
I have a renderer for iOS and set the following:
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.BackgroundColor = UIColor.Red;
return cell;
}
总的来说,我的整个ListView
使用的是我为列表视图设置的Gray
背景.我想发生的是ListView
具有Gray
彩色背景(这意味着标题部分将具有灰色背景),而Red
具有Red
彩色背景.我是否设置了错误的属性来获取TextCell
的所需背景?
Somehow my whole ListView
is using the Gray
background I set for the listview. 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
. Am I setting the wrong property to get the desired background for TextCell
?
推荐答案
您的自定义渲染器实际上没有返回单元格,因此您对BackgroundColor
所做的更改没有通过.
Your custom renderer is not actually returning the cell, so the change you make to the BackgroundColor
is not coming through.
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
.
Update: You should be setting cell.ContentView.BackgroundColor
instead.
这篇关于如何在Xamarin Forms中的ListView内更改TextCell的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!