1
votes

I am trying to change background-color of selected item of a listbox. it changes background-color but next time if I select another item, then background-color of previous selected item should set to null or default.

Listbox name is lstContacts.

private void lstContacts_SelectionChanged(object sender, SelectionChangedEventArgs e)

lstContacts.ClearValue(ListBox.BackgroundProperty);//its not working   
 ListBoxItem item = lstContacts.ItemContainer.ContainerFromItem(lstContacts.SelectedItem) as ListBoxItem;
item.Background = new SolidColorBrush(Colors.Red);

can anyone help me?

3
I am facing same issue, did you got any solution for this ?Kinjan Bhavsar

3 Answers

3
votes

Make use of the index of the item

var item = ListBox_Main.Items[0] as ListBoxItem ;
item.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 255, 0))
1
votes

It's simple. Reset the whole list view background color before changing the background color of another item.

lstContacts.Background = new SolidColorBrush("your original color here");

Then proceed as

ListBoxItem item = lstContacts.ItemContainer.ContainerFromItem(lstContacts.SelectedItem) as ListBoxItem;
item.Background = new SolidColorBrush(Colors.Red);
0
votes

Selected items already have background color set whenever you select it, either programmatically or by tapping on item.

How do you set the background color for the selected item? Have you changed the ControlTemplate for your ListBox?