I'm trying to developp an App in Xamarin.Forms and XAML, and I need to capture an event when I tap on a LV. What I want the app to do is change the color of the Cell (or a StackLayout inside the cell) when the user tap on the Item on a list. I'm trying to implement the OnItemTapped property on the listview, but nothing happens when I press the Item. My code is quite simple:
Model: Product with 2 propeties, name and color.
public class Product: INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public Product ()
{
}
public string Name { get; set; }
public bool Buy { get; set; }
public Color micolor { get; set; }
//Método para actualizar los cambios
protected virtual void OnPropertyChanged (string propertyName)
{
if (PropertyChanged == null)
return;
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
XAML: Only a list with two bindings: the name in a label, and the color in the SL
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="pruebalv.MyPage1">
<ContentPage.Content>
<ListView x:Name="listView"
ItemTapped="OmItemTapped"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout
Orientation="Vertical"
BackgroundColor ="{Binding micolor}">
<Label Text="{Binding Name}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
And behind .cs code: I create the products (it is only a sample) and implement the on item tapped. When item tapped, I want to change the color property of the item pressed.
public void OmItemTapped (object o, ItemTappedEventArgs e)
{
var dataItem = e.Item as Product;
dataItem.micolor = Color.Green;
dataItem.Name = "Tapped!!";
}
The result that I get when I create a list is that nothing happens when I tap on an item.
Do am I missing something? As I've understood with the tutorials, only with the "itemtapped" roperty of the ListView, and the InotifyProperty... on the products, I'd see the results. Is it correct? Thanks!