我想为多种数据类型设计一个通用的radzen数据网格。这意味着在剃刀页面上,我有radzen数据网格的通用标记。记录集和列定义动态绑定到网格。我的问题是"TItme"。
我需要类似的东西:
TItem="typeof(data.records[0])"
数据网格如下:
<RadzenDataGrid FilterMode="FilterMode.Simple" Data="@data.records" TItem="typeof(data.records[0])" AllowFiltering="@data.allowFiltering" AllowPaging="@data.allowPaging" AllowSorting="@data.allowSorting">
<Columns>
@foreach (var colDef in data.columnDefinitions)
{
<RadzenDataGridColumn TItem="typeof(data.records[0])" Property="@colDef.columnProperty" Title="@colDef.columnTitle" />
}
</Columns>
</RadzenDataGrid>
您可能想在dotnet 6:中尝试动态组件
<DynamicComponent Type="@componentType" Parameters="@parameters" />
@code {
private Type componentType = ...;
private IDictionary<string, object> parameters = ...;
}
文档:https://learn.microsoft.com/en-us/aspnet/core/blazor/components/dynamiccomponent?view=aspnetcore-6.0
其他dotnet版本的另一个选择是尝试直接在代码中定义渲染片段(注意带有ATTENTION注释的行(:
@page "/built-content"
<h1>Build a component</h1>
<div>
@CustomRender
</div>
<button @onclick="RenderComponent">
Create three Pet Details components
</button>
@code {
private RenderFragment? CustomRender { get; set; }
private RenderFragment CreateComponent() => builder =>
{
for (var i = 0; i < 3; i++)
{
builder.OpenComponent(0, typeof(PetDetails));
builder.AddAttribute(1, "PetDetailsQuote", "Someone's best friend!");
builder.AddAttribute(2, "TItem", typeof(data.records[0])); // ATTENTION
builder.CloseComponent();
}
};
private void RenderComponent()
{
CustomRender = CreateComponent();
}
}
文档:https://learn.microsoft.com/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-3.1