[英]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.