C# 基于bindingcontext删除ListView项
我创建了一个从SQLite数据库填充的listview。XML如下所示:C# 基于bindingcontext删除ListView项,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我创建了一个从SQLite数据库填充的listview。XML如下所示: <ListView x:Name="CalculationListview" ItemsSource="{Binding Calculation}" HasUnevenRows="true"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> &l
<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>
上下文动作
我建议将删除功能移动到
当用户在ViewCell上从右向左滑动时,上下文操作将出现在iOS上;当用户长按ViewCell时,上下文操作将出现在Android上
示例屏幕截图
此屏幕截图来自Xamarin.Forms文档,不反映以下代码
代码
async void Handle\u Delete(对象发送方,System.EventArgs e)
{
var viewCellSelected=发送方作为菜单项;
var calculationToDelete=viewCellSelected?.BindingContext作为计算;
等待App.Database.DeleteCalculationSync(calculationToDelete);
}
看起来您应该执行类似于var selectedItem=Calculation.Single(x=>x.Id==Calculation.Id)的操作代码>这将从绑定中为您提供正确的值。我不确定我是否可以在该上下文中执行Calculation.single()
public Task<int> DeleteCalculationAsync(Calculation calculation)
{
return database.DeleteAsync(calculation);
}
void Handle_Clicked(object sender, System.EventArgs e)
{
App.Database.DeleteCalculationAsync(SOMETHING HERE);
}