- Home
- Forum
- Xamarin.Forms
- SfListView tap item programmatically
SfListView tap item programmatically
Hello
I am using Syncfusion.Xamarin NuGet packages v17.4.0.55.
I try to accomplish a programmatic tap on a SfListView item that behaves exactly the same as when manually tapping the item on the device itself.
Please note that I use MVVM.
For setting the items to select, I would use the following:
myListView.SelectedItems.Add(newItem);
However, this does not seem to work as expected:
- The UI does not set the background color of the selected item (a workaround can be found here https://www.syncfusion.com/forums/130721/selected-color-not-going-over-item-when-putting-background-color-on-data-template)
- The SfListView seems to ignore the items that I set programmatically and on tapping on one of these, the SelectionChanged event contains the item in the "AddedItems" list instead of the "RemovedItems" list
Now my question is, if there is any way to mimic the manual tap exactly (including the highlighting in the ui and the SfListView "knowing" which items are already added)?
Kind regards
Phil
SIGN IN To post a reply.
1 Reply
LN
Lakshmi Natarajan
Syncfusion Team
March 19, 2020 06:04 PM UTC
Hi Phil,
Thank you for using Syncfusion support.
We have checked the reported query “SfListView tap item programmatically” from our end.
#Regarding the UI does not set the background color of the selected item
We would like to let you know that you can select an ListViewItem by the following ways,
- Setting SelectedItem property
|
private void Button_Clicked(object sender, EventArgs e)
{
var bc = (sender as Button).BindingContext as ContactsViewModel;
bc.SelectedItem = viewModel.contactsinfo[0];
} |
- Adding item to the SelectedItems collection
|
private void Button_Clicked(object sender, EventArgs e)
{
var bc = (sender as Button).BindingContext as ContactsViewModel;
listView.SelectedItems.Add(bc.contactsinfo[0]);
} |
In both cases, the UI will be updated as expected and the background color will be changed.
Please refer SfListView selection in our online documents from following link,
#Regarding the SelectionChanged event contains the item in the "AddedItems" list instead of the "RemovedItems" list
We would like to let you know that when selecting, the selected item is added in the AddedItems collection. While deselecting, the deselected item will be added in the RemovedItems collection.
|
ItemSelectionChangedEventArgs |
Description |
|
AddedItems |
Gets collection of the underlying data objects where the selection has been processed.
|
|
RemovedItems |
Gets collection of the underlying data objects where the selection has been processed.
|
Please refer SfListView selection in our online documents from following link,
#Regarding highlighting in the ui and the SfListView "knowing" which items are already added
You can achieve your requirement using the above solution to highlight the selection. Also, you can get the information of SelectedItem from AddedItems.
We have already documented this scenario in our online document. Please refer the following link,
KB link: https://www.syncfusion.com/kb/9961/how-to-notify-item-selection-using-mvvm-in-xamarin-forms-listview
We have attached the sample based on your requirement in the following link,
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewXamarin1641341151
Please check the sample and let us know if you still facing the same issue? If not, please modify our sample to reproduce the issue and revert us back with the following details which would be helpful for us to check on it and provide you the solution at the earliest.
- Share issue reproducing video
- Share ListView related code snippet
- Share code snippet of programmatic tap
Lakshmi Natarajan
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
UN Unknown
- Mar 18, 2020 10:31 AM UTC
- Mar 19, 2020 06:04 PM UTC