BuildCssClass 方法
重写以实现个性化的 CSS 逻辑代码
Button.razor
@inherits BlazorComponentBase
<button @attributes="@AdditionalAttributes">@ChildContent</button>
@code{
[Parameter]public RenderFragment? ChildContent { get; set; }
[CascadingParameter]EditContext? CascadedEditContext { get; set; }
protected override void BuildCssClass(ICssClassBuilder builder)
{
if(CascadedEditContext is not null)
{
builder.Append("modified");
var valid = CascadedEditContext.GetValidationMessages().Any();
if(valid)
{
builder.Remove("invalid");
}
}
}
}
Button.cs
public class Button : BlazorComponentBase, IHasChildContent
{
[Parameter]public RenderFragment? ChildContent { get; set; }
[CascadingParameter]EditContext? CascadedEditContext { get; set; }
protected override void BuildCssClass(ICssClassBuilder builder)
{
if(CascadedEditContext is not null)
{
builder.Append("modified");
var valid = CascadedEditContext.GetValidationMessages().Any();
if(valid)
{
builder.Remove("invalid");
}
}
}
}
Last updated