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

MudBlazor中DataGrid内MudRadioGroup异常:单选按钮全选

问题原因

你遇到的问题是因为所有行的MudRadioGroup都绑定了同一个全局变量SelectedOption,当你选择任意一行的单选按钮时,这个全局变量的值会被更新,所有行的单选组都会同步这个值,导致全部行的单选按钮都显示为选中状态。

解决方案

要解决这个问题,需要让每一行的单选组绑定当前行数据的独立属性,同时确保数据对象的属性可以被修改:

  1. 将Employee从不可变的record改为可修改的class(或使用record的with表达式更新,这里用class更直观)
  2. 让每个MudRadioGroup绑定当前行context.Item的ispresent属性
  3. 调整回调方法,针对当前员工对象更新属性

修改后的完整代码:

@inject ISnackbar Snackbar

<MudDataGrid Items="@employees" Filterable="false" SortMode="@SortMode.None" Groupable="false">
    <Columns>
        <PropertyColumn Property="x => x.Name" />
        <TemplateColumn CellClass="d-flex justify-end">
            <CellTemplate>
                <MudStack Row>
                    <MudRadioGroup T="string" @bind-Value="@context.Item.ispresent" 
                                   SelectedOptionChanged="(val) => OnSelectedOptionChanged(val, context.Item)">
                        <MudRadio Option="Yes" Value="Yes" Color="Color.Primary">Yes</MudRadio>
                        <MudRadio Option="No" Value="No" Color="Color.Secondary">No</MudRadio>
                    </MudRadioGroup>
                </MudStack>
            </CellTemplate>
        </TemplateColumn>
    </Columns>
</MudDataGrid>

@code {
    // 将record改为class,允许修改ispresent属性
    public class Employee 
    {
        public string Name { get; set; }
        public string Position { get; set; }
        public int YearsEmployed { get; set; }
        public int Salary { get; set; }
        public int Rating { get; set; }
        public string ispresent { get; set; }

        public Employee(string name, string position, int yearsEmployed, int salary, int rating, string ispresent)
        {
            Name = name;
            Position = position;
            YearsEmployed = yearsEmployed;
            Salary = salary;
            Rating = rating;
            this.ispresent = ispresent;
        }
    }

    public List<Employee> employees;

    protected override void OnInitialized()
    {
        employees = new List<Employee>
        {
            new Employee("Sam", "CPA", 23, 87_000, 1,"No"),
            new Employee("Alicia", "Product Manager", 11, 143_000, 5,"Yes"),
            new Employee("Ira", "Developer", 4, 92_000, 3,"No"),
            new Employee("John", "IT Director", 17, 229_000, 4,"No"),
        };
    }

    // 回调方法接收选中值和当前员工对象,更新对应属性
    private void OnSelectedOptionChanged(string selectedOption, Employee employee)
    {
        employee.ispresent = selectedOption;
        Snackbar.Add($"员工 {employee.Name} 的状态改为: {selectedOption}");
    }
}
关键改动说明
  • 数据对象修改:把Employee从record改为class,因为record的属性默认是只读的,无法直接修改ispresent值;如果坚持用record,可以在回调中用employees = employees.Select(e => e == employee ? e with { ispresent = selectedOption } : e).ToList();来更新集合。
  • 绑定目标调整:每个MudRadioGroup的@bind-Value绑定到context.Item.ispresent,确保每行的单选状态独立。
  • 回调方法优化:新增Employee参数,明确修改当前行对应的数据,同时在Snackbar中显示具体员工的状态变化,更直观。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:26:19