How the first item change LabelTextColor is White and SelectionBackgroundColor=Red in SFlistView

When loading for the first time

I default first item is selectdItem, but LabelTextColor and SelectionBackgroundColor not change.

when I ItemTapped the color will change

.

What should I do?



ListViewXamarin

9999.jpg



private void ListView_SelectionChanging(object sender, ItemSelectionChangingEventArgs e)

{

if (listView.SelectionMode == Syncfusion.ListView.XForms.SelectionMode.Single)

{ if (e.AddedItems.Count > 0)

{

var item = e.AddedItems[0] as Model; item.LabelTextColor = Color.White;

}

if (e.RemovedItems.Count > 0) {

var item = e.RemovedItems[0] as Model; item.LabelTextColor = Color.Black;

}

}

}


1 Reply

LN Lakshmi Natarajan Syncfusion Team April 18, 2022 06:47 AM UTC

Hi Lin,


To select the item programmatically, we must assign the underlying object from the collection to the SelectedItem. Please refer to the following code snippets for more reference,


public void GenerateInfo()

{

    Random r = new Random();

    for (int i = 0; i < 20; i++)

    {

        var contact = new Contacts(CustomerNames[i], r.Next(720, 799).ToString() + " - " + r.Next(3010, 3999).ToString(),i==0 ? Color.White:Color.Black);

 

        contact.ContactImage = ImageSource.FromResource("ListViewXamarin.Images.Image" + r.Next(0, 28) + ".png");

        contactsinfo.Add(contact);

 

        if (i == 0)

        {

            SelectedItem = contactsinfo[i];

        }

    }

}


UG link: https://help.syncfusion.com/xamarin/listview/selection#programmatic-selection


Please let us know if you need further assistance.


Regards,

Lakshmi Natarajan


Loader.
Up arrow icon