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

.NET MAUI实现ListView多选时选中项自动勾选CheckBox

解决方案

步骤1:更新实体类,实现属性变更通知

给ComandeTable添加标记选中状态的布尔属性,并实现INotifyPropertyChanged接口,确保属性变化能同步到UI:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class ComandeTable : INotifyPropertyChanged
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string ReferenceComande { get; set; }   
    public string DesignationComande { get; set; } 

    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                OnPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

步骤2:修改XAML布局

  • 给ListView设置SelectionMode="Multiple"开启多选支持
  • 将CheckBox的IsChecked属性绑定到实体类的IsSelected属性
<ListView x:Name="listViewComande"                  
          ItemSelected="listViewArticle_ItemSelected"
          SelectionMode="Multiple">
    <ListView.Header>        
    </ListView.Header>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid RowDefinitions="Auto" 
                      ColumnDefinitions="5*,20*,45*,10*,10*,10*" 
                      Padding="3,10,3,10"  ColumnSpacing="8">
                    <CheckBox Grid.Row="1" Grid.Column="0" 
                              Color="LightGray"                     
                              IsChecked="{Binding IsSelected, Mode=TwoWay}" />                  
                    <Label Grid.Row="1" Grid.Column="2"  
                           Text="{Binding DesignationComande}"      
                           FontSize="13" 
                           VerticalTextAlignment="Center"
                           HorizontalTextAlignment="Start" 
                           MaxLines="1"  
                           FontAttributes="None"/>                  
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

步骤3:完善ItemSelected事件方法

在选中项变更时,切换对应实体的IsSelected属性:

private void listViewArticle_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{   
    if (e.SelectedItem is ComandeTable selectedItem)
    {
        // 切换选中状态:已勾选则取消,未勾选则勾选
        selectedItem.IsSelected = !selectedItem.IsSelected;
        // 若不需要保留ListView的选中视觉反馈,可取消注释下方代码清除选中
        // listViewComande.SelectedItem = null;
    }
}

额外说明

  • 双向绑定会让手动勾选CheckBox也同步更新实体的IsSelected属性,若需要同步ListView选中状态,可额外处理CheckBox的CheckedChanged事件
  • 批量获取选中项时,直接遍历listViewComande.SelectedItems集合,或筛选数据源中IsSelected=true的项即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:57:28