RadListView Set SelectedItem from code

1 Answer 85 Views
ListView (obsolete)
Joe
Top achievements
Rank 2
Iron
Veteran
Iron
Joe asked on 08 Nov 2024, 09:17 PM

I have a RadListView I am setting up to replace the standard Maui ListView.

One thing I need to do is set the Selected item in the ViewModel as part of a filter so filtered item is already selected.  This seems to be an issue with doing this in the RadListView.


<telerik:RadListView x:Name="RadListView" 
                     BackgroundColor="Transparent" VerticalOptions="Fill"
                     ItemsSource="{Binding SymbolItems}"
                     SelectionMode="Single" SelectionGesture="Tap" IsGroupHeaderSticky="True"
                     SelectedItem="{Binding SelectedSymbolItem, Mode=TwoWay}" >

I setup the SelectedItem to fire off the Notify (this uses Prism base class)

public SymbolItem SelectedSymbolItem
{
	get => _selectedSymbolItem;
	set => SetProperty(ref _selectedSymbolItem, value);
}

But when I set the SelectedItem I get an exception

Value cannot be null. (Parameter 'indexPath')
   at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:line 28
   at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle(INativeObject self, String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:line 42
   at TelerikUI.TKListView.SelectItem(NSIndexPath indexPath, Boolean animated, UICollectionViewScrollPosition scrollPosition)
   at Telerik.Maui.Controls.Compatibility.DataControlsRenderer.iOS.ListViewRenderer.SelectItem(Object item)
   at Telerik.Maui.Controls.Compatibility.DataControls.RadListView.OnSelectedItemAdded(Object item)
   at Telerik.Maui.Controls.Compatibility.DataControls.RadListView.OnSelectedItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectedItemsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)
   at System.Collections.ObjectModel.ObservableCollection`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].InsertItem(Int32 index, Object item)
   at Telerik.Maui.Controls.Compatibility.DataControls.ListView.SelectedItemsCollection.InsertItem(Int32 index, Object item)
   at System.Collections.ObjectModel.Collection`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Add(Object item)
   at Telerik.Maui.Controls.Compatibility.DataControls.RadListView.UpdateSelectedItem()
   at Telerik.Maui.Controls.Compatibility.DataControls.RadListView.OnPropertyChanged(String propertyName)

The value being set is definitely not null.

Also I notice that the Setter seems to be getting invoked twice, so if I put a breakpoint my code goes in and executes the setter, and then seems like the framework does something and invokes it again which throws the exception.

Can SelectedItem in a RadListView be set be using the bound property? (like with any general TwoWay binding).  If not how could this be achieved?

1 Answer, 1 is accepted

Sort by
1
Didi
Telerik team
answered on 11 Nov 2024, 01:30 PM

Hello Joe,

Yes, you can use the selected item and can set it programmatically. I have tested in a sample. Maybe I am missing something from the setup. Send me sample project where the issue occurs.

Regarding to the ListView usage, with the upcoming release this week 11-15 November 2024, the RadListView will be obsolete, so I can suggest you migrate to the new control - RadCollectionView

The RadCollectionView is a complete rewrite of the ListView from the ground up. CollectionView offers improved performance, enhanced features, and a modernized approach to managing lists of data. The CollectionView incorporates all key features of the ListView.

Regards,
Didi
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Joe
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 12 Nov 2024, 04:30 PM

Didi,

Thanks for the reply.  So I switched to the CollectionView and I have everything wired up and is working except for one issue I am running into.

In our app if groups have only one item the are expanded by default otherwise the group is collapsed.  I implemented this using with an EventAggregator that sends a notification to my View code behind which, generally, works as desired

eventAggregator.GetEvent<EditLayersLoadedEvent>().Subscribe(() =>
{
	try
	{
		var dataView = RadCollectionView.GetDataView();

		while ( !dataView.IsDataReady )
		{
		}

		var groups = dataView.GetGroups();

		var items = dataView.Items;
		if ( items.Count == 0 )
		{
			Console.WriteLine("Huh?");
		}

		foreach (var dataGroup in groups)
		{
			if ( dataGroup.ChildItems.Count > 1 )
			{
				dataView.CollapseGroup(dataGroup);
			}
			else
			{
				dataView.ExpandGroup(dataGroup);
			}
		}
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}
 

I am having an issue in one scenario....

In the app we filter the list down to a group with one item.  The filtering works fine and I can refresh the bound ObservableCollection.  What is odd, though, is that while the ObservableCollection contains the one item, when I publish the event the DataView does not yet 'see' the items.  That is my reason for adding IsDataReady check and also that conditional for zero items.  So I even put code in the CollectionChanged handler hoping that would be later in the refresh process

SymbolItems.CollectionChanged += (sender, args) =>
{
	if ( _isListFiltered )
	{
		EventAggregator.GetEvent<EditLayersLoadedEvent>().Publish();
		ExecuteSymbolClicked(SelectedSymbolItem);
	}
};

I can add a button just for testing to fire my event and that works.  So it seems like a timing issue that the ObservableCollection in the ViewModel has updated, however, that update is not yet propagated down to the RadCollectionView.

How can I find out when the RadCollectionView items has changed so that I can fire off this refresh at the right time?

Thanks

-Joe

Didi
Telerik team
commented on 13 Nov 2024, 07:40 AM

Hi Joe,

I am glad to hear the SelectedItem behavior is solved using the RadCollectionView. 
Regarding to the grouping, this is another questions. I would like to ask you to open new threads for each questions you have. Also I can suggest you to use the support ticketing system and submit your questions there. You can attach sample repro projects for the reported behaviors. If you do not want to attach your code to the forum, you can use the tickets. 

Now direct to the question, I am not sure I understand the issue. I have attached my sample, update it to match the exact behavior you have, so I can further research the case. 

Tags
ListView (obsolete)
Asked by
Joe
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Didi
Telerik team
Share this question
or