I have my ListView containing list of objects called “Item”
I want that once an Item is clicked, change it’s background color to a different color.
My issue is that I can’t access a specific cell. I can access the Item using list.selectedItem
but not the cell containing him
this.list = new ListView
{
ItemsSource = new ObservableCollection<Item>(Lists.Items),
ItemTemplate = new DataTemplate(() =>
{
Label nameLabel = Lists.settings.CreateLabel();
Label descriptionLabel = Lists.settings.CreateLabel();
Button addButton = new Button
{
Text = "+",
FontSize = 16,
BackgroundColor = Color.FromHex(Settings.LightModeColors[0]),
TextColor = Colors.Black,
};
Button deductButton = new Button
{
Text = "-",
FontSize = 16,
BackgroundColor = Color.FromHex(Settings.DarkModeColors[0]),
TextColor = Colors.White,
};
Label quantityLabel=Lists.settings.CreateLabel("0");
nameLabel.SetBinding(Label.TextProperty, nameof(Item.Name));
descriptionLabel.SetBinding(Label.TextProperty, nameof(Item.Description));
addButton.Clicked += async (sender, args) =>
{
quantityLabel.Text = "" + (int.Parse(quantityLabel.Text) + 1);
Debug.WriteLine(args.GetType());
AddItem("id");
};
deductButton.Clicked += async (sender, args) =>
{
if (quantityLabel.Text != "0")
{
quantityLabel.Text = "" + (int.Parse(quantityLabel.Text) - 1);
DeductItem(nameof(Item.Id));
}
};
Grid templateLayout = new Grid
{
RowDefinitions =
{
new RowDefinition{Height=new GridLength(1,GridUnitType.Star) },
},
ColumnDefinitions =
{
new ColumnDefinition{Width=new GridLength(3,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(2,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
new ColumnDefinition{Width=new GridLength(1,GridUnitType.Star) },
},
};
templateLayout.Add(nameLabel, 0, 0);
templateLayout.Add(descriptionLabel, 1, 0);
templateLayout.Add(addButton, 2, 0);
templateLayout.Add(quantityLabel, 3, 0);
templateLayout.Add(deductButton, 4, 0);
return new ViewCell { View = templateLayout };
})
};
list.ItemTapped += OnItemTapped;
private void OnItemTapped(object sender, ItemTappedEventArgs e)
{
//list.SelectedItem = null;
Debug.WriteLine(sender.GetType());
Debug.WriteLine(((ListView)sender).SelectedItem.GetType());
Debug.WriteLine(e.GetType());
Debug.WriteLine(e.ItemIndex.GetType());
Debug.WriteLine(e.Item.GetType());
}
tried debugging to find the some type of View object inside listview