最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

blazor - How do we reference the HeaderTemplate to specify the HTML using RenderFragment for the QuickGrid? - Stack Overflow

programmeradmin4浏览0评论

We have a .NET 8 & C# Blazor project in VS 2022 with a number of QuickGrids.

I want to place HTML and styling for the PropertyColumn's header text using the HeaderTemplate that will override the Title field. But I don't know how to use the HeaderTemplate.

My QuickGrid markup looks like this:

<PropertyColumn 
         Property="f => f.TXT_NAME"              
         HeaderTemplate="<TemplateHeader>Missouri</TemplateHeader>" 
         Sortable="false" Class="itemName" />

I have googled and search for a sample of the correct way to specify/utilize the HeaderTemplate capability, but could not find any samples. What you see in my code above is a non-working GUESS of how to use the HeaderTemplate.

When I type the code for HeaderTemplate, VS 2022 immediately follows that with ="" -- implying some text should be typed in. BUT... I have tried various things and you can see my attempt at providing a component that contains a render fragment -- that will not compile with an error

An invalid expression term '<'

I am so stuck.

The render fragment component markup and code are as follows:

<div class="text-danger fst-italic fw-bold">
   @ChildContent
</div>

@code {
#pragma warning disable CS8618
   [Parameter] public RenderFragment? ChildContent { get; set; }
#pragma warning restore CS8618
}
      

Your solutions, comments and questions are welcome.

Thanks.

We have a .NET 8 & C# Blazor project in VS 2022 with a number of QuickGrids.

I want to place HTML and styling for the PropertyColumn's header text using the HeaderTemplate that will override the Title field. But I don't know how to use the HeaderTemplate.

My QuickGrid markup looks like this:

<PropertyColumn 
         Property="f => f.TXT_NAME"              
         HeaderTemplate="<TemplateHeader>Missouri</TemplateHeader>" 
         Sortable="false" Class="itemName" />

I have googled and search for a sample of the correct way to specify/utilize the HeaderTemplate capability, but could not find any samples. What you see in my code above is a non-working GUESS of how to use the HeaderTemplate.

When I type the code for HeaderTemplate, VS 2022 immediately follows that with ="" -- implying some text should be typed in. BUT... I have tried various things and you can see my attempt at providing a component that contains a render fragment -- that will not compile with an error

An invalid expression term '<'

I am so stuck.

The render fragment component markup and code are as follows:

<div class="text-danger fst-italic fw-bold">
   @ChildContent
</div>

@code {
#pragma warning disable CS8618
   [Parameter] public RenderFragment? ChildContent { get; set; }
#pragma warning restore CS8618
}
      

Your solutions, comments and questions are welcome.

Thanks.

Share Improve this question edited Jan 19 at 6:50 marc_s 754k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 18 at 21:30 John DJohn D 6872 gold badges10 silver badges27 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

You can use the HeaderTemplate on a PropertyColumn

<PropertyColumn Title="Date" Property="p => p.Date"> 
    <HeaderTemplate>
        Test
    </HeaderTemplate>
</PropertyColumn>

you can also just use css to target the header via isolation Weather.razor.css

::deep div.col-title-text {
    color: orangered;
}

However if the QuickGrid is a root component the ::deep operator will not work.

<enable-deep>
    <QuickGrid Items="forecasts.AsQueryable()">
        <PropertyColumn Title="Date" Property="p => p.Date" />
        <PropertyColumn Title="Temp. (C)" Property="p => p.TemperatureC" />
        <PropertyColumn Title="Temp. (F)" Property="p => p.TemperatureF" />
        <PropertyColumn Title="Summary" Property="p => p.Summary" />
    </QuickGrid>
</enable-deep>


Here I use a custom HTML element to enable the ::deep selector. The Browser ignores the element.

FYI: PropertyColumn inherits ColumnBase which has the RenderFragment parameter HeaderTemplate

Official docs on RenderFragment use.

You need to use aTemplateColumn rather than a PropertyColumn.

Here's an example for the Weather Page:

<QuickGrid TGridItem="WeatherForecast" ItemsProvider="this.GridItemsProvider">
    <PropertyColumn Property="(r) => r.Date" Title="Date" />
    <TemplateColumn>
        <HeaderTemplate>
            <h6>Temp &degf</h6>
        </HeaderTemplate>
        <ChildContent>
            @context.TemperatureC
        </ChildContent>
    </TemplateColumn>
    <PropertyColumn Property="(r) => r.TemperatureF" Title="Temp &degf" />
    <PropertyColumn Property="(r) => r.Summary" Title="Summary" />
</QuickGrid>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论