簡體   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