我创建了一个从SQLite DB填充的列表视图 . XML看起来像这样:
<ListView x:Name="CalculationListview" ItemsSource="{Binding Calculation}" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Qty}"></Label>
<Label Text="{Binding Note}"></Label>
<Button Text="Delete" Clicked="Handle_Clicked"></Button>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我已经创建了一个按钮,如果单击该按钮,我想从中删除该项目 .
我已经从数据库中删除了一个接收给定对象的方法 .
public Task<int> DeleteCalculationAsync(Calculation calculation)
{
return database.DeleteAsync(calculation);
}
不幸的是,我不知道如何从bindingcontext中获取对象,以便我可以删除该项 . 我显然已经点击了事件处理程序:
void Handle_Clicked(object sender, System.EventArgs e)
{
App.Database.DeleteCalculationAsync(SOMETHING HERE);
}
2 回答
上下文操作
我建议将删除功能移至Context Actions .
当用户长按ViewCell时,当用户在ViewCell上从右向左滑动时,上下文操作将出现在iOS上 .
示例屏幕截图
此屏幕截图来自Xamarin.Forms文档,并未反映下面的代码 .
代码
如果您不想使用
ContextActions
或't because you have to follow some design requirements you could add a binding to the button' sStyleId
来计算类别中的ID或其他内容来识别计算列表中的项目,然后根据按钮单击事件中的项目删除计算 .然后在您的点击事件中