Binding SelectedItems throws an exception

Dear support Team


Binding the ListView Selected Items throws an Error as soon the selections is changed.

Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

  at Syncfusion.ListView.XForms.SelectionController.ProcessMultipleSelection (Syncfusion.ListView.XForms.ListViewItemInfoBase itemInfo, System.Object itemdata) [0x00019] in <a23e73410d084cd6b84613a8e2def977>:0

  at Syncfusion.ListView.XForms.SelectionController.ProcessSelection (Syncfusion.ListView.XForms.ListViewItemInfoBase itemInfo, System.Object itemdata) [0x00031] in <a23e73410d084cd6b84613a8e2def977>:0

  at Syncfusion.ListView.XForms.SelectionController.HandleTouchInteraction (Syncfusion.ListView.XForms.TouchGesture gesture, Syncfusion.ListView.XForms.ListViewItemInfoBase itemInfo, Xamarin.Forms.Point position) [0x000d8] in <a23e73410d084cd6b84613a8e2def977>:0

  at Syncfusion.ListView.XForms.ListViewItemInfoBase.HandleTouchInteraction (Syncfusion.ListView.XForms.TouchGesture gesture, Xamarin.Forms.Point position) [0x0008d] in <a23e73410d084cd6b84613a8e2def977>:0

  at Syncfusion.ListView.XForms.Android.ListViewItemRenderer.OnClick (Android.Views.View view) [0x00086] in <7006034e31104b119b70b80fa946db95>:0

  at Syncfusion.ListView.XForms.Android.ListViewItemRenderer.OnSingleTapUp (Android.Views.MotionEvent e) [0x00000] in <7006034e31104b119b70b80fa946db95>:0

  at Android.Views.GestureDetector+IOnGestureListenerInvoker.n_OnSingleTapUp_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x00010] in /Users/xtmq/xamarin/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.Views.GestureDetector.cs:697

  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.59(intptr,intptr,intptr)

I did the binding like explained in the documentation. What is the issue here?


"1.0" encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xForms="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
x:Class="ClassicXamarin.Mobile.ListViewPage" x:Name="ThisPage">
<ContentPage.BindingContext>
<x:Reference Name="ThisPage">x:Reference>
ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label Text="New User"/>
<Entry/>
<Button Text="Add item to list"/>
<Label Text="All Users"
FontSize="Large"/>
<xForms:SfListView ItemsSource="{Binding Users}"
SelectedItems="{Binding SelectedItems}"
SelectionMode="Multiple"/>
<Label Text="Selected Users"
FontSize="Large"/>
<xForms:SfListView ItemsSource="{Binding SelectedItems}"/>
StackLayout>
ContentPage.Content>
ContentPage>
using System.Collections.ObjectModel;
using Xamarin.Forms.Xaml;

namespace ClassicXamarin.Mobile
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListViewPage
{
ObservableCollection<string> _users;
ObservableCollection<object> _selectedItems;

public ListViewPage()
{
InitializeComponent();

Users = new ObservableCollection<string>
{
"Hans",
"Fritz",
"Manuel"
};
}

public ObservableCollection<string> Users
{
get => _users;
set
{
_users = value;
OnPropertyChanged(nameof(Users));
}
}

public ObservableCollection<object> SelectedItems
{
get => _selectedItems;
set
{
_selectedItems = value;
OnPropertyChanged(nameof(SelectedItems));
}
}
}
}

The whole Project is under:

https://github.com/PatrickRainer/PrismApp


Thank you for your Feedback.


br Patrick



9 Replies

LN Lakshmi Natarajan Syncfusion Team August 30, 2021 12:20 PM UTC

Hi Patrick, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Binding SelectedItems throws an exception” from our side. We would like to let you know that the SelectedItems collection isn't initialized, which results in an exception when you try to select an item. You can resolve the reported exception by initializing the SelectedItems collection. 
 
Please refer to the following code snippets for more reference, 
public ListViewPage() 
{ 
    InitializeComponent(); 
 
    Users = new ObservableCollection<string> 
    { 
        "Hans", 
        "Fritz", 
        "Manuel" 
    }; 
 
    SelectedItems = new ObservableCollection<object>(); 
} 
 
You can also refer to our guidance document regarding the same, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 



PA Patrick September 1, 2021 04:34 AM UTC

Dear Kakshimi


Sorry my bad in the sample.


Now, can I bind the SelectedItems to a property in my model? So like:

<xForms:SfListView SelectionMode="Multiple"
ItemsSource="{Binding BooksProvider}"
SelectedItems="{Binding Person.Books}"


The model would be like this:

public class Person
{
public string Name { get; set; }
public List<Book> Books { get; set; } = new List<Book>();
}

public class Book
{
public string ISBN { get; set; }
public string Title { get; set; }

public override string ToString()
{
return Title;
}
}


Github Sample: https://github.com/PatrickRainer/SfListViewSample

Br Patrick



LN Lakshmi Natarajan Syncfusion Team September 1, 2021 12:20 PM UTC

Hi Patrick, 
 
Thank you for the update. 
 
We have checked the reported query “can I bind the SelectedItems to a property in my model?” from our side. We would like to inform you that you can bind the complex properties to the SfListView.SelectedItems property.  
 
You can overcome the reported scenario by using the ObservableCollection instead of the List. Since, the ObservableCollection provides notification when item gets added dynamically. Also, we suggest you to use the ObservableCollection type as object, as mentioned in our user guidance document. 
 
Please refer to the following code snippets to achieve your requriement, 
public class Person 
{ 
    public string Name { get; set; } 
    public ObservableCollection<object> Books { get; set; } = new ObservableCollection<object>(); 
} 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan


PA Patrick September 3, 2021 04:07 AM UTC

Ok, thank you that works so far, but is a bit complicated.


Is it possible to use a "ValueConverter" for this case?


br Patrick



LN Lakshmi Natarajan Syncfusion Team September 3, 2021 07:16 AM UTC

Hi Patrick, 
 
Thank you for the update. 
 
We'd like to let you know that the List does not notify the UI when a dynamic collection changes. So, if we want the UI to respond based on dynamic collection changes, we recommend that you use an ObservableCollection that implements the INotifyCollectionChanged interface. Furthermore, the converter will not work with List because List does not notify collection changes. 
 
You can also refer to the following discussion regarding the same, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
  
 



PA Patrick September 3, 2021 03:07 PM UTC

Thank you.

That the list does not implement the INotifyCollectionchanged interface is clear.


What I mean is, if use an "ObservableCollection<Book>" and not an "ObservableCollection<object>" in the model. Is it then possible to use a "ValueConverter"?


Br Patrick



LN Lakshmi Natarajan Syncfusion Team September 6, 2021 07:08 AM UTC

Hi Patrick, 
 
Thank you for the update. 
 
#Regarding if use an "ObservableCollection<Book>" and not an "ObservableCollection<object>" in the model. Is it then possible to use a "ValueConverter"? 
 
We have checked the reported query from our side. We would like to let you know that the converter will be called whenever the bound property changes. Furthermore, the converter will function if the selected item is added during the initialization process. When the collection is modified, however, the converter does not receive a notification. As a result, changes made during run time will not be reflected. 
 
Because you updated the collection after initialization (dynamically) in your scenario, the converter will not work. As a result, we recommend that you use the generic collection for the SelectedItems property. 
 
Regards, 
Lakshmi Natarajan 



PA Patrick September 7, 2021 04:01 AM UTC

Ok, thank you for this explanation.


br Patrick




LN Lakshmi Natarajan Syncfusion Team September 7, 2021 05:51 AM UTC

Hi Patrick, 
 
Thank you for the update. 
 
We are glad that the provided solution meets your requirement. Please let us know if you need any further assistance. As always, we are happy to help you out.  
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon