I have a question regarding a listview in Xamarin.Forms
binded my listview succesfully with some items but i want to change the background color of the selected cell how do i do this in Xamarin.Forms
I make use of
var cell = DataTemplate(typeof(ImageCell));
ListView listView = new ListView
{
SeparatorColor = Color.Green,
ItemsSource = ListlvData,
ItemTemplate = cell, // Set the ImageCell to the item templatefor the listview
};
Edit:
To add the below code to a ListView.DataTemplate
, you would want to do something like this:
ListView listView = new ListView {
SeparatorColor = Color.Green,
ItemsSource = ListlvData
};
listView.ItemTemplate = new DataTemplate(() => {
ViewCell cell = new ViewCell();
cell.Tapped += (sender, args) => {
cell.View.BackgroundColor = Color.Red;
OnListViewTextCellTapped(cell); //Run your actual `Tapped` event code
cell.View.BackgroundColor = Color.Default; //Turn it back to the default color after your event code is done
};
cell.View = new Image();
return cell;
});
To change the background color on Tapped
you will need to use a ViewCell
and an Image
control within that since ImageCell
does not support BackgroundColor
s by default.
I put a StackLayout
within the ViewCell
but then on the Tapped
event, I change the ViewCell.View
's BackgroundColor
, like so:
ViewCell cell = new ViewCell();
cell.Tapped += (sender, args) => {
cell.View.BackgroundColor = Color.Red;
OnListViewTextCellTapped(cell); //Run your actual `Tapped` event code
cell.View.BackgroundColor = Color.Default; //Turn it back to the default color after your event code is done
};