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

MAUI iOS页面初始加载时Radio Button无法显示选中状态

问题:CollectionView中的RadioButton无法默认选中

我在应用里有个数据展示页面,里面的RadioButton放在CollectionView中,每行都会显示。按照需求,页面加载时应该有一个按钮处于选中状态,但不管怎么设置,所有RadioButton都显示未选中。

RadioButton的XAML定义

<RadioButton x:Name="RbSelect" Grid.Column="6" Grid.Row="0"
 CheckedChanged="RadioButton_CheckedChanged"
 GroupName="RadioGroup" IsChecked="{Binding RbSelected}">
 <RadioButton.Content>
     <StackLayout>
         <Label
             Margin="30,0,0,0"                                            
             TextColor="{StaticResource FieldNameTextColor}"
             VerticalOptions="Start" />
     </StackLayout>
 </RadioButton.Content>
</RadioButton>

我尝试的设置代码(最新版本,试过多种方案)

public UpdateCars(ObservableCollection<AutoWithSwitch> value, CollectionView updateCarsViewModel)
{           
    InitializeComponent();           
    updateCarsViewModel.ItemsSource = null;           
    
    foreach (var item in value)
    {
        if (item.IsChecked)
            RbSelect.IsChecked = true;
        else
            RbSelect.IsChecked = true;
    }
    updateCarsViewModel.ItemsSource = value;
}

解决方案

1. 修正数据源操作逻辑

你当前代码存在两个核心问题:

  • 遍历数据时,无论item.IsChecked是true还是false,都将RbSelect.IsChecked设为true,逻辑完全错误;
  • CollectionView会为每个数据项生成独立的RadioButton实例,你直接操作模板中的RbSelect控件毫无意义,因为它不是运行时的实际控件。

正确做法是直接修改数据源中对应项的RbSelected属性(也就是RadioButton绑定的属性):

public UpdateCars(ObservableCollection<AutoWithSwitch> value, CollectionView updateCarsViewModel)
{           
    InitializeComponent();           
    
    foreach (var item in value)
    {
        // 根据你的业务逻辑,将IsChecked映射到绑定的RbSelected属性
        item.RbSelected = item.IsChecked;
    }
    
    updateCarsViewModel.ItemsSource = value;
}

2. 确保数据模型实现属性变更通知

AutoWithSwitch类必须实现INotifyPropertyChanged接口,否则属性修改无法同步到UI,RadioButton不会更新选中状态:

public class AutoWithSwitch : INotifyPropertyChanged
{
    private bool _rbSelected;
    private bool _isChecked;

    public bool RbSelected
    {
        get => _rbSelected;
        set
        {
            if (_rbSelected != value)
            {
                _rbSelected = value;
                OnPropertyChanged(nameof(RbSelected));
            }
        }
    }

    public bool IsChecked
    {
        get => _isChecked;
        set
        {
            if (_isChecked != value)
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

3. 检查RadioButton分组设置

如果所有RadioButton共用同一个GroupName="RadioGroup",那么同一时间只能有一个按钮被选中。如果需要每行的RadioButton独立可选,需要将GroupName绑定到每行的唯一标识(比如数据项的ID):

<RadioButton x:Name="RbSelect" Grid.Column="6" Grid.Row="0"
 CheckedChanged="RadioButton_CheckedChanged"
 GroupName="{Binding Id}" IsChecked="{Binding RbSelected}">

4. 遵循数据驱动原则

在Xamarin.Forms/MAUI中,控件状态应该由数据源驱动,而非直接操作控件。避免在页面初始化阶段操作CollectionView中的子控件,因为此时控件尚未实例化完成。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 15:45:10