C# 如何使用listview xamarin为每行数据库上的删除记录设置x按钮
我在listview中添加了城市名称。我有一个输入ID的字段和一个删除名称的按钮。是否有一个选项,可以在每行添加的名称旁边放置一个X按钮来删除该名称,而无需手动添加ID并按下按钮 我的xaml代码:C# 如何使用listview xamarin为每行数据库上的删除记录设置x按钮,c#,sqlite,xamarin,C#,Sqlite,Xamarin,我在listview中添加了城市名称。我有一个输入ID的字段和一个删除名称的按钮。是否有一个选项,可以在每行添加的名称旁边放置一个X按钮来删除该名称,而无需手动添加ID并按下按钮 我的xaml代码: <StackLayout HorizontalOptions="Center" VerticalOptions="Start"> <Label Margin="0,0,0,10&quo
<StackLayout HorizontalOptions="Center" VerticalOptions="Start">
<Label Margin="0,0,0,10" Text="SQLite" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
<Entry x:Name="txtPersonId" Placeholder="PersonId Update and Delete"></Entry>
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button x:Name="btnDelete" WidthRequest="200" Text="Delete" Clicked="BtnDelete_Clicked" />
</StackLayout>
<ListView x:Name="lstPersons">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding PersonID}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
如何使用listview xamarin为每行数据库上的删除记录设置x按钮
您可以先看一下,然后看一下下面的代码:
<ListView x:Name="lstPersons">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding PersonID}" />
<Label HorizontalOptions="CenterAndExpand" Text="{Binding Name}" />
<Button
Clicked="Button_Clicked"
HorizontalOptions="EndAndExpand"
Text="Delete a record" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
使用ViewCell而不是TextCell,并在ViewCell中添加所需的任何UI
<ListView x:Name="lstPersons">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding PersonID}" />
<Label HorizontalOptions="CenterAndExpand" Text="{Binding Name}" />
<Button
Clicked="Button_Clicked"
HorizontalOptions="EndAndExpand"
Text="Delete a record" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private void Button_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var person = btn.BindingContext as personclass;
//delete this item.
//......
}