You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为MudButtonGroup中选中状态的MudButton设置样式?

为MudButtonGroup选中按钮设置样式的方案

方案1:通过绑定值手动添加样式类

给每个按钮绑定条件类,根据选中状态切换样式:

<MudButtonGroup @bind-SelectedValue="selectedOption">
    <MudButton Value="opt1" Class="@(selectedOption == "opt1" ? "selected-btn" : "")">选项1</MudButton>
    <MudButton Value="opt2" Class="@(selectedOption == "opt2" ? "selected-btn" : "")">选项2</MudButton>
</MudButtonGroup>

<style>
.selected-btn {
    background: #2563eb;
    color: white;
    border-color: #2563eb;
}
</style>

@code {
    private string selectedOption = "opt1";
}

方案2:利用MudBlazor内置的激活类

MudButtonGroup选中按钮时会自动添加mud-button-active类,直接用这个类写CSS即可:

.mud-button-group .mud-button.mud-button-active {
    background-color: #2563eb;
    color: #fff;
    border-color: #2563eb;
}

如果是特定变体(比如Outlined),可以更精准定位:

.mud-button-group .mud-button-outlined.mud-button-active {
    background-color: rgba(37, 99, 235, 0.1);
    border-color: #2563eb;
    color: #2563eb;
}

方案3:通过MudBlazor主题全局配置

在项目的主题设置中统一修改按钮激活状态样式:

var mudTheme = new MudTheme
{
    Components = new Dictionary<string, ComponentStyle>()
    {
        {
            nameof(MudButton), new ComponentStyle()
            {
                Styles = new Dictionary<string, string>()
                {
                    { "Active", "background-color: #2563eb; color: white;" }
                }
            }
        }
    }
};

内容的提问来源于stack exchange,提问作者Demet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 17:15:02