简体   繁体   English

如何在Xamarin表单的ListView中更改TextCell的背景颜色?

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

相关问题 更改 Xamarin Forms 上 ListView 项的背景颜色 - Change the Background Color of an ListView Item on Xamarin Forms 如何在 Xamarin 表单 NavigationPage 中更改背景颜色 - How to change backGround color in Xamarin forms NavigationPage xamarin形式:无法更改ListView上所选项目的背景颜色 - xamarin forms: Couldn't change the background color of selected item on ListView 无法在 Xamarin.forms 中更改 CollectionView 中框架的背景颜色 - Not able to change the Background color of Frame inside CollectionView in Xamarin forms 如何在没有自定义渲染器的情况下更改 Xamarin Forms Cross Platform 中列表视图所选项目的背景颜色 - How to change background color of listview selected item in Xamarin Forms Cross Platform without a custom renderer listview xamarin.forms中selecteditem的背景颜色 - Background color of selecteditem in listview xamarin.forms 如何更改listview所选项目的文本颜色Xamarin.forms - How to change the listview selected item text color Xamarin.forms 在Xamarin.Forms中将命令传递到DataTemplate内部的TextCell - Passing a Command to a TextCell inside a DataTemplate in Xamarin.Forms 如何更改导航 Titleview 的背景颜色 - Xamarin Forms? - How to change background color of navigation Titleview - Xamarin Forms? Xamarin形成更改导航栏的背景颜色 - Xamarin forms change background color of navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM