简体   繁体   English

Xamarin.Forms在ListView中的条目更改时获取ListView的对象

[英]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. 我有一个项目类,其名称和价格值存储在SQLite数据库中。 I'm displaying them in a Listview with a Label for the name and an Entry for the price. 我在带有名称标签和价格条目的Listview中显示它们。

<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. 我要这样做,以便在完成条目更改后将使用新的价格值更新SQLite数据库中的项目,但是,我不知道如何从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
}

in your updateItem handler, then BindingContext of the item should be element from the ItemSource that is being updated 在您的updateItem处理程序中,则该项目的BindingContext应该是要更新的ItemSource中的元素

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM