如何在Xamarin Forms中的ListView内更改TextCell的背景颜色? [英] How to change the background color of `TextCell` inside `ListView` in Xamarin Forms?

查看:266
本文介绍了如何在Xamarin Forms中的ListView内更改TextCell的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆