按行项目条件更改 MudBlazor 表背景颜色

T0b*_*0bi 11 c# blazor mudblazor

我正在尝试更改 mudblazor 表中一行的颜色。问题是,我无法添加根据行元素的条件更改颜色的功能。

 <MudTable Items="@context.orderPositions" Context="AddressContext" Hover="true" Breakpoint="Breakpoint.Sm" Elevation="0" Style="background-color: @(AddressContext.OrderPositionStatus == PositionStatus.rdy ? yellowgreen : blue;">
Run Code Online (Sandbox Code Playgroud)

Nad*_*ury 23

你可以:

<MudTable Items="@Items" Hover="true" RowStyleFunc="RowStyleFunc">
Run Code Online (Sandbox Code Playgroud)

进而

private string RowStyleFunc(Item arg1, int index)
{
    switch (arg1.Title)
    {
        case string a when a.Contains("1/4"):
            return "background-color:blue";
        case string b when b.Contains("2/4"):
            return "background-color:red";          
        default: return "background-color:white";

    }
}
Run Code Online (Sandbox Code Playgroud)


T0b*_*0bi 0

我有一个解决方案,但感觉有点脏......

<RowTemplate>
    <MudTd DataLabel="Menge" Style="@(AddressContext.OrderPositionStatus == PositionStatus.rdy ? $"background:{Colors.BlueGrey.Darken4};" : $"background:{Colors.Cyan.Darken1};")">@AddressContext.orderItem.ItemQuantity</MudTd>
    <MudTd DataLabel="Itemcode" Style="@(AddressContext.OrderPositionStatus == PositionStatus.rdy ? $"background:{Colors.BlueGrey.Darken4};" : $"background:{Colors.Cyan.Darken1};")">@AddressContext.orderItem.ItemCode</MudTd>
    <MudTd DataLabel="Itemname" Style="@(AddressContext.OrderPositionStatus == PositionStatus.rdy ? $"background:{Colors.BlueGrey.Darken4};" : $"background:{Colors.Cyan.Darken1};")">@AddressContext.orderItem.ItemName</MudTd>
</RowTemplate>
Run Code Online (Sandbox Code Playgroud)

如图所示,可以更改行模板内的颜色。所以上下文是可用的。对于每一行/列组合来说,这都是烦人的工作,但最终它还是有效的。