cvanbeek cvanbeek - 4 years ago 214
C# Question

Xamarin.Forms Get the object of a ListView when an Entry in the ListView is changed

I have an item class with values for name and price which are stored in an SQLite database. I'm displaying them in a Listview with a Label for the name and an Entry for the price.

<ListView x:Name="itemListView">
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label Text="{Binding name}"/>
<Entry Text="{Binding Path=price, Mode=TwoWay, StringFormat='{}{0:c}'}" Keyboard="Numeric" Completed="updateItem" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView>


I want to make it so when the Entry change is completed it will update the item in the SQLite database with the new value for price, however, I don't know how to retrieve the Item which was finished from the Listview. I think I need to use the following method but I don't know what to put in it.

async void updateItem(object sender, EventArgs e)
{
//Code to update the item with the SQLite database with the new price
}

Answer Source

in your updateItem handler, then BindingContext of the item should be element from the ItemSource that is being updated

async void updateItem(object sender, EventArgs e)
{
    var item = (MyItemType) ((Entry)sender).BindingContext;

    //Code to update the item with the SQLite database with the new price
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download