Reputation: 103
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?
Upvotes: 1
Views: 11316
Reputation: 163
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);
Upvotes: 1
Reputation: 7122
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
?
Upvotes: 0
Reputation: 222722
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))
Upvotes: 3