---
description: 重写以实现个性化的 CSS 逻辑代码
---

# BuildCssClass 方法

{% code title="Button.razor" %}
```cshtml
@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");
            }
        }
    }
}
```
{% endcode %}

{% code title="Button.cs" %}
```csharp
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");
            }
        }
    }
}
```
{% endcode %}
