Xamarin.forms 在iOS上删除ListView组标题的背景色

Xamarin.forms 在iOS上删除ListView组标题的背景色,xamarin.forms,Xamarin.forms,我在ListView中启用了分组,组标题的背景颜色为浅灰色。 如何将其删除\使其透明?您可以将背景色设置为白色。只需将网格或StackLayout放入ViewCell,并在ListView的GroupHeaderTemplate的DataTemplate中使用ViewCell!例如: <ListView.GroupHeaderTemplate> <DataTemplate> <ViewCell Height="25">

我在ListView中启用了分组,组标题的背景颜色为浅灰色。
如何将其删除\使其透明?

您可以将背景色设置为白色。只需将网格或StackLayout放入ViewCell,并在ListView的GroupHeaderTemplate的DataTemplate中使用ViewCell!例如:

<ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell Height="25">
                <Grid BackgroundColor="#ffffff" VerticalOptions="FillAndExpand">
                    <Label Text="{Binding Title}" />
                </Grid>
            </ViewCell>
        </DataTemplate>
</ListView.GroupHeaderTemplate>


您可以查看此页面以了解更多信息:

您必须通过渲染器执行此操作

我认为默认情况下会添加背景色,因为iOS组标题会粘贴在视图的顶部,单元格会滑到其下方。因此,如果没有背景色,标题的文本就会浮在单元格上,看起来有点傻

[assembly: ExportRenderer(typeof(ViewCell), typeof(TransparentViewCellRenderer))]
namespace MyProject.iOS.Renderers
{
    public class TransparentViewCellRenderer : ViewCellRenderer
    {
        public TransparentViewCellRenderer()
        {

        }

        public override UITableViewCell GetCell(Cell item, UITableView tv)
        {
            var cell = base.GetCell(item, tv);
            if (cell != null) cell.BackgroundColor = UIColor.Clear;
            return cell;
        }
    }
}

iOS设置单元格实例的默认背景色。您可以通过以下方式进行更改:


带有渲染器的解决方案不再有效

对于希望使用自定义渲染器的用户,需要使用SetBackgroundColor方法,而不是设置UITableViewCell的BackgroundColor属性

[assembly: ExportRenderer(typeof(ViewCell), typeof(TransparentViewCellRenderer))]
namespace MyProject.iOS.Renderers
{
    public class TransparentViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell pCell, UITableViewCell pReusableCell, UITableView pTableView)
        {
            UITableViewCell lCell = base.GetCell(pCell, pReusableCell, pTableView);
            if (lCell != null)
            {
                SetBackgroundColor(lCell, pCell, UIColor.Clear);
            }
            return lCell;
        }
    }
}

是否可以添加当前使用的代码?覆盖组标题template@cvanbeek没什么特别的,我只是将
IsGroupingEnable=“True”
GroupHeaderTemplate
设置为带有标签的数据模板。在iOS上,组标题的背面为浅灰色。为什么需要用网格包裹标签?标签也有BackgroundColor,我想?标签没有BackgroundColor属性,除非您使用格式化文本。无论如何,如果您通过formattedtext设置标签的背景色,最终将得到标签大小的彩色框。但你的问题是给你的头球涂上背景色。因此,您必须使用堆栈布局或网格。
Label
具有
BackgroundColor
属性。标签源于
视图
,而视图又源于
可视化元素
。James Montenagmo的只是一些快速脏代码\示例代码,我们不应该将其作为参考。在网格或标签上使用
BackgroundColor
是一种非常难看的方式,使其看起来像是在更改节标题的背景色,但实际上并非如此。
[assembly: ExportRenderer(typeof(ViewCell), typeof(TransparentViewCellRenderer))]
namespace MyProject.iOS.Renderers
{
    public class TransparentViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell pCell, UITableViewCell pReusableCell, UITableView pTableView)
        {
            UITableViewCell lCell = base.GetCell(pCell, pReusableCell, pTableView);
            if (lCell != null)
            {
                SetBackgroundColor(lCell, pCell, UIColor.Clear);
            }
            return lCell;
        }
    }
}