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

.NET 7 MAUI:如何通过代码选中CollectionView项并禁用用户选择

解决.NET 7 MAUI CollectionView禁用手动选择但保留代码选中样式的问题

这里提供两种可行方案,你可以根据需求选择:


方案1:拦截用户点击事件,保留系统默认选中样式

这种方案继续依赖系统的SelectedItem和选中样式,但通过拦截用户点击事件阻止手动选中操作,代码设置选中的逻辑不受影响。

修改XAML代码

在ItemTemplate的根布局(如Grid)中添加TapGestureRecognizer,用来拦截点击事件:

<CollectionView
    SelectionMode="Single"
    ItemsSource="{Binding Items}" 
    SelectedItem="{Binding SelectedItem}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid ColumnDefinitions="*, Auto">
                <Grid.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnItemTapped" />
                </Grid.GestureRecognizers>
                
                <Label 
                    VerticalOptions="EndAndExpand"
                    Text="{Binding FullName}" />
                <HorizontalStackLayout Grid.Column="1">
                    <Label Text="{Binding Time}"/>
                    <Label Text="{Binding TimePostfix}"/>
                </HorizontalStackLayout>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

添加后台事件处理

在页面的代码-behind文件中实现OnItemTapped方法,标记事件为已处理,阻止CollectionView触发选中逻辑:

private void OnItemTapped(object sender, EventArgs e)
{
    // 拦截点击事件,不让CollectionView触发手动选中
    ((TappedEventArgs)e).Handled = true;
}

方案2:自定义选中样式,完全脱离系统SelectionMode控制

这种方案不依赖系统的选中机制,通过给数据模型添加自定义选中属性,自己控制选中样式,同时设置SelectionMode="None"彻底禁用手动选中。

1. 更新数据模型

给你的Item模型添加IsSelected属性(需实现INotifyPropertyChanged):

public class ItemModel : INotifyPropertyChanged
{
    // 原有属性:FullName、Time、TimePostfix、IsSelectedNext
    
    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            _isSelected = value;
            OnPropertyChanged();
        }
    }

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

2. 修改ViewModel选中逻辑

替换原有的SelectedItem赋值逻辑,改为设置对应项的IsSelected属性:

// 先找到目标项
var targetItem = Items.FirstOrDefault(x => x.IsSelectedNext);
// 同步所有项的选中状态
foreach (var item in Items)
{
    item.IsSelected = item == targetItem;
}

3. 修改CollectionView的XAML

设置SelectionMode="None",并通过DataTrigger绑定自定义选中样式:

<CollectionView
    SelectionMode="None"
    ItemsSource="{Binding Items}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid ColumnDefinitions="*, Auto">
                <Grid.Style>
                    <Style TargetType="Grid">
                        <Setter Property="BackgroundColor" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger TargetType="Grid" Binding="{Binding IsSelected}" Value="True">
                                <!-- 自定义选中时的背景色,可替换为其他样式 -->
                                <Setter Property="BackgroundColor" Value="#E0E0E0"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                
                <Label 
                    VerticalOptions="EndAndExpand"
                    Text="{Binding FullName}" />
                <HorizontalStackLayout Grid.Column="1">
                    <Label Text="{Binding Time}"/>
                    <Label Text="{Binding TimePostfix}"/>
                </HorizontalStackLayout>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:57:08