简体   繁体   English

如何在 blazor 的基础组件中覆盖渲染片段

[英]How to override a render fragment in a base component in blazor

I am using the Blazorise DataGrid and I wanted to make a custom DataGridColumn where DisplayTemplate is pre set to a template however I cant figure out how I am supposed to set the DisplayTemplate if I derive from the DataGridColumn.我正在使用 Blazorise DataGrid,我想制作一个自定义 DataGridColumn,其中 DisplayTemplate 预先设置为模板,但是如果我从 DataGridColumn 派生,我无法弄清楚我应该如何设置 DisplayTemplate。 I started with this我从这个开始

   @typeparam TItem
   @inherits DataGridColumn<TItem>

But then I had no clue how to set the DisplayTemplate render fragment to a razor snippet.但是后来我不知道如何将 DisplayTemplate 渲染片段设置为剃刀片段。

I also tried instead just making a component that had a DataGridColumn in it and referenced that in my DataGrid but then the column was always at the end regardless of where I put it in the DataGrid.我也尝试过只制作一个包含 DataGridColumn 的组件,并在我的 DataGrid 中引用它,但是无论我将它放在 DataGrid 中的哪个位置,该列始终位于末尾。

I am probably barkign up the wrong tree but I have a lot of classes that implement interfaces where I will always want to set the DisplayTemplate the same for a specific column in any data grid for any type that implements that interface.我可能是错误的树,但我有很多实现接口的类,我总是希望为实现该接口的任何类型的任何数据网格中的特定列设置相同的 DisplayTemplate。 So it seemed reasonable to make a DataGridColumn derived type for that purpose.因此,为此目的创建 DataGridColumn 派生类型似乎是合理的。

You can fill DisplayTemplate in your derived class within the code section:您可以在代码部分的派生类中填充 DisplayTemplate :

@typeparam TItem
@inherits DataGridColumn<TItem>

@{
    base.BuildRenderTree(__builder);
}


@code { 
    protected override void OnInitialized()
    {
        base.OnInitialized();
        this.DisplayTemplate = (TItem item) => @<CustomDisplayTemplate T="TItem" Item="@item" />;
    }
}

For this to work you'll want to define a separate Blazor component called CustomDisplayTemplate:为此,您需要定义一个名为 CustomDisplayTemplate 的单独 Blazor 组件:

@typeparam T
<h2>@Item.ToString()</h2>

@code {
    [Parameter]
    public T Item { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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