BuildCssClass Method

This method can be override for customization css string with logical code

Override BuildCssClass method to create component CSS string with logical code

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