繁体   English   中英

如何在Xamarin表单的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渲染器,并设置了以下内容:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM