We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Binding SelectedItems on Multiple selection throws exception

I am using MVVM and trying to bind slecteditems twoway mode. If I bind selecteditems from Viewmodel to View by raising Property changed, It throws exception as below

  <sfListView:SfListView x:Name="listItems"   SelectedItems="{Binding SelectedItems, Mode=TwoWay}"                 
                           ItemsSource="{Binding Items}"  SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}"             
                             SwipeOffset="250"  BackgroundColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                          SelectionMode="Multiple"  
                                           ItemSpacing="1"  IsStickyHeader="False" IsStickyFooter="False"   
                   AllowSwiping="True" IsEnabled="True">



  private ObservableCollection<Database.Model.Item> selectedItems;

        public ObservableCollection<Database.Model.Item> SelectedItems
        {
            get
            { 
                return selectedItems;
            }
            set
            {
                if (value != selectedItems && ListSelectionMode == SelectionMode.Multiple)
                {
                    selectedItems = value;
                    RaisePropertyChanged("SelectedItems");
                }
            }
        }



10-02 00:55:08.832 I/MonoDroid(24942): System.NullReferenceException: Object reference not set to an instance of an object.
10-02 00:55:08.832 I/MonoDroid(24942):   at Syncfusion.ListView.XForms.SfListView.OnSelectionModeChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00015] in <eb04770f88c84a24bd134736ce89a543>:0 
10-02 00:55:08.832 I/MonoDroid(24942):   at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:592 
10-02 00:55:08.832 I/MonoDroid(24942):   at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:386 
10-02 00:55:08.832 I/MonoDroid(24942):   at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:173 
10-02 00:55:08.832 I/MonoDroid(24942):   at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:55 
10-02 00:55:08.832 I/MonoDroid(24942):   at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__47_0 () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindingExpression.cs:539 
10-02 00:55:08.832 I/MonoDroid(24942):   at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 
10-02 00:55:08.832 I/MonoDroid(24942):   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 
10-02 00:55:08.832 I/MonoDroid(24942):   at (wrapper dynamic-method) System.Object:69d2077b-b265-45f9-a8c2-7c7704266070 (intptr,intptr)
 

5 Replies

DB Dinesh Babu Yadav Syncfusion Team October 2, 2017 10:44 AM UTC

Hi Emil,   
     
Sorry for the inconvenience caused.     
    
Based on the given information, we have checked the reported issue and a support incident has been created under your account to track the status of the issue. Please log on to our support website to check for further updates.          
    
    
Regards,    
Dinesh Babu Yadav 



DA Damian April 23, 2018 04:08 PM UTC

Hello,

Same issue today. Can we bind to SelectedItems?
Property SelectedItems has only getter. 
Syncfusion: this control is great! You doing well:) but can you provide usage of SelectedItems in MVVM manner?  

Damian

This code throw exception:

 
                    x:Name="Vendors"
                    ItemsSource="{Binding Vendors}" SelectionMode="Multiple"
                    SelectedItems="{Binding SelectedVendors}"
                    SelectionChanged="SfListView_SelectionChanged"
                    VerticalOptions="Start" Orientation="Horizontal">

[0:] System.NullReferenceException: Object reference not set to an instance of an object.
  at Syncfusion.ListView.XForms.SelectionController.ProcessItemsSourceChanged (System.Object oldValue, System.Object newValue) [0x00040] in :0 
  at Syncfusion.ListView.XForms.SfListView.OnItemsSourceChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0001d] in :0 


RS Rawoof Sharief Muthuja Sherif Syncfusion Team April 24, 2018 03:51 PM UTC

Hi Damian, 
 
We have checked the reported query “Binding collection of items to SelectedItems property throws null reference exception” from our side. We would like to let you know that SelectedItems property uses ObservableCollection of type ‘object’. So you need to specify generic object instead of model class as like below. 
 
Code Example[C#]: 
        private ObservableCollection<object> selectedItems; 
 
        public ObservableCollection<object> SelectedItems 
        { 
            get 
            { 
                return selectedItems; 
            } 
            set 
            { 
                if (value != selectedItems && ListSelectionMode == SelectionMode.Multiple) 
                { 
                    selectedItems = value; 
                    RaisePropertyChanged("SelectedItems"); 
                } 
            } 
        } 
  
Please let us know if you require further assistance. 
 
Regards, 
Rawoof.M 



PP Paul Parkins December 19, 2018 09:10 AM UTC

This same caveat has just caught me out too, and changing my bound SelectedItems property type to ObservableCollection<Object> as suggested did resolve my problem.
In my opinion this is not clear enough in the online documentation. Yes the example shows a collection of Object, but the text does not state that it HAS to be of Object. The implication is that through inheritance, any type of class (inherited from Object) could be used. It seems this is not the case, and so should be stated.

This documentation does give the example, but should be improved as it does not state you have to use Object.
This document doesn't even discuss SelectedItems enough to warrant adding in the caveat.

Also, did you realise SelectedItems is not even offered by Intellisense (am using VS2017)?

Paul



JN Jayaleshwari N Syncfusion Team December 20, 2018 12:14 PM UTC

Hi Emil,  
 
We have checked reported query from our side. You can bind the selected items property in xaml page but intelligence will not work in xaml is because we have used private set to set the collection for SelectedItems in our source to avoid inner exception in output window.  
 
Regarding documentation: 
We have logged task to update the required details regarding SelectedItems type. We will update the UG documentation shortly in live.

 
Regards,  
Jayaleshwari N. 


Loader.
Up arrow icon