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

.NET MAUI中如何获取选中CheckBox的名称与值

实现方案

1. 定义Office数据模型

首先给Office数据添加IsSelected属性,用于绑定CheckBox的选中状态:

public class Office : INotifyPropertyChanged
{
    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            _isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }

    public string OfficeName { get; set; }

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

2. ViewModel核心逻辑

在ViewModel中维护Office集合,实现Continue按钮的点击处理逻辑:

public class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Office> Offices { get; set; } = new ObservableCollection<Office>();
    public ICommand ContinueCommand { get; }

    public MainViewModel()
    {
        ContinueCommand = new Command(ExecuteContinue);
        // 替换为实际接口加载逻辑
        LoadOfficesFromApi();
    }

    private void LoadOfficesFromApi()
    {
        // 模拟接口返回数据
        Offices.Add(new Office { OfficeName = "ADMISSION" });
        Offices.Add(new Office { OfficeName = "CASHIER" });
        Offices.Add(new Office { OfficeName = "REGISTRATION" });
    }

    private void ExecuteContinue()
    {
        // 提取所有选中的OfficeName并转为数组
        string[] selectedOfficeNames = Offices.Where(o => o.IsSelected)
                                             .Select(o => o.OfficeName)
                                             .ToArray();

        // 这里可添加后续业务逻辑,比如提交数据、打印等
        foreach (var name in selectedOfficeNames)
        {
            Console.WriteLine($"选中部门:{name}");
        }
    }

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

3. XAML布局(CollectionView结合CheckBox)

页面XAML中绑定ViewModel与CollectionView:

<ContentPage.BindingContext>
    <local:MainViewModel />
</ContentPage.BindingContext>

<StackLayout Padding="20">
    <CollectionView ItemsSource="{Binding Offices}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <HorizontalStackLayout Margin="0,5">
                    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                    <Label Text="{Binding OfficeName}" Margin="10,0" VerticalOptions="Center" />
                </HorizontalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

    <Button Text="Continue" Command="{Binding ContinueCommand}" Margin="0,20" />
</StackLayout>

关键说明

  • 模型和ViewModel必须实现INotifyPropertyChanged接口,保证UI与数据双向同步。
  • 点击Continue按钮时,通过LINQ筛选出IsSelected为true的Office实例,提取OfficeName转换为数组。
  • 若为WPF项目,只需将XAML中的CollectionView替换为ListBox/ItemsControl,其余控件用法一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 00:55:29