如何从 ViewModel 获取 CollectionView.SelectedItems

问题描述 投票:0回答:1

我想从视图模型中的

CollectionView
获取所选项目。

我尝试将 SelectedItems 绑定到视图模型中 ObservableCollection 类型的属性,并将

Mode
设置为
TwoWay
,但它给了我绑定失败,错误消息是“无法将 ObservableCollection 类型隐式转换为 IList”,所以我将属性更改为 IList 仍然得到“无法将类型 IList 隐式转换为 IList”,因此我制作了一个转换器:

using CommunityToolkit.Maui.Converters;
using System.Collections.ObjectModel;
using System.Globalization;

namespace Portal.ValueConverters
{
    internal class UserObservableCollectionToIListOfObjects : BaseConverter<ObservableCollection<User>, IList<object>>
    {
        public override IList<object> DefaultConvertReturnValue { get => []; set => throw new NotImplementedException(); }
        public override ObservableCollection<User> DefaultConvertBackReturnValue { get => []; set => throw new NotImplementedException(); }

        public override ObservableCollection<User> ConvertBackTo(IList<object> value, CultureInfo culture)
        {
            ObservableCollection<User> users = value as ObservableCollection<User>;
            return users;
        }

        public override IList<object> ConvertFrom(ObservableCollection<User> value, CultureInfo culture)
        {
            IList<object> objects = value as IList<object>;
            return objects;
        }
    }
}

但是当我选择任何项目时,视图模型中的属性计数仍然为 0。 CollectionView 看起来像这样:

<CollectionView SelectedItems="{Binding SelectedMembers,Mode=TwoWay,Converter={StaticResource Key=UserObservableCollectionToIListOfObjectsConverter}}" ItemsSource="{Binding Friends}" SelectionMode="Multiple" BackgroundColor="#222">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="models:User">
            <HorizontalStackLayout Padding="20">
                <ImageButton Source="{Binding ImageSource}" CornerRadius="25" WidthRequest="50" HeightRequest="50" HorizontalOptions="Center"/>
                <Label Text="{Binding Name}" FontSize="Title" Margin="5" VerticalOptions="Center"/>
            </HorizontalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

我已经在代码隐藏页面构造函数中设置了

BindingContext
。 视图模型如下所示:

    public partial class SelectGroupMembersViewModel:ObservableObject
    {
        private readonly IUserFriendshipService _userFriendshipService;

        public Command GetUserFriendsCommand { get; }
        public Command GetSelectedMembersCommand { get; }
        public ObservableCollection<User> SelectedMembers { get; set; }
        public ObservableCollection<User> Friends { get; }
        public SelectGroupMembersViewModel(IUserFriendshipService userFriendshipService)
        {
            _userFriendshipService = userFriendshipService;

            GetUserFriendsCommand = new(async () => await GetUserFriendsAsync());
            GetSelectedMembersCommand = new(async () => await GetSelectedMembersAsync());

            Friends = [];
            SelectedMembers = [];
        }
        public async Task GetUserFriendsAsync()
        {
            IList<User> userFriends = await _userFriendshipService.GetCurrentUserFriendsAsync();

            Friends.Clear();
            foreach (User friend in userFriends)
                Friends.Add(friend);
        }
}

并且“GetUserFriendsCommand”由

ContentPage
Loaded
EventToCommandBehavior
事件中运行。 那么有什么解决方案或解决方法吗

c# list mvvm maui observablecollection
1个回答
0
投票

这可能不是最有效或最聪明的解决方案,但是您是否考虑过向 User 类添加布尔 IsSelected 属性并在选择项目时更新该布尔值?

然后,当您想要获取所有选定的成员时,您可以过滤 IsSelected 为 true 的 Friends 集合。

public class User
{
    // Existing properties
    public bool IsSelected { get; set; }
}

// Update the selection logic in the CollectionView's item tapped event
private void OnItemTapped(object sender, ItemTappedEventArgs e)
{
    var selectedUser = e.Item as User;
    if (selectedUser != null)
    {
        selectedUser.IsSelected = !selectedUser.IsSelected;
    }
}

// In ViewModel, get selected members like this
public ObservableCollection<User> GetSelectedMembers()
{
    return new ObservableCollection<User>(Friends.Where(user => user.IsSelected));
}
© www.soinside.com 2019 - 2024. All rights reserved.