xamarin/Xamarin.Forms

[Bug] CollectionView ItemSelected only binds to List<object> and not to custom objects

オープン

#8,155 opened on 2019/10/22

 (0 件のコメント) (6 件のリアクション) (0 人の担当者)C# (1,926 件のフォーク)batch import
a/collectionviewexcellent-reporthelp wantedin-progressinactivet/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

Description

When binding List<FilterOption> (which is a custom object) to the SelectedItems property of the CollectionView, the pre-selection doesn't work properly. It only works if you bind the SelectedItems property to a List<object>. The online documentation also uses a List<object> to bind to the SelectedItems property of a CollectionView, but I was expecting to be able to use my own custom objects inside a List (or ObservableCollection) and properly bind to the SelectedItems property of a CollectionView.

Steps to Reproduce

  1. Bind the ItemsSource and SelectedItems properties of your CollectionView to a property in your ViewModel.
<CollectionView SelectionMode="Multiple" ItemsSource="{Binding Options}" SelectedItems="{Binding SelectedOptions}">
...
</CollectionView>
  1. Add the following properties to your ViewModel
        private ObservableCollection<FilterOption> _options = new ObservableCollection<FilterOption>();
        public ObservableCollection<FilterOption> Options
        {
            get => _options;
            set
            {
                SetProperty(ref _options, value); // using the Prism BindableBase SetProperty() method here
                RaisePropertyChanged(nameof(SelectedOptions));
            }
        }

        // works
        //public List<object> SelectedOptions
        //{
        //    get => new List<object>(Options?.Where(o => o.IsSelected));
        //}

        // doesn't work
        public List<FilterOption> SelectedOptions
        {
            get => new List<FilterOption>(Options?.Where(o => o.IsSelected));
        }

Note: FilterOption is just a class with 2 properties: Title and IsSelected

Expected Behavior

Binding the SelectedItems to a List<FilterOption> highlights (selects) the items inside the CollectionView.

Actual Behavior

Highlighting of the SelectedItems doesn't work, nor does it throw a decent exception to be told that you should bind to a List<object> instead.

Basic Information

  • Version with issue: Xamarin.Forms v4.3.0.908675 (thus stable version of CollectionView and not the experimental version, which also has the same behavior)
  • Last known good version: unknown
  • IDE: Visual Studio 2019 v16.3.5
  • Platform Target Frameworks:
    • iOS: 11
    • Android: 9
    • UWP: N/A
  • Android Support Library Version: N/A
  • Nuget Packages: N/A
  • Affected Devices: tested on multiple devices and OS

Screenshots

Non working example (binding to List<FilterOption>) Non working example

Working example (binding to List<object>) Working example

Reproduction Link

XForms.CollectionView.MultiPreSelect.zip

コントリビューターガイド