I have listview to which I add the names of cities. I have a field to enter ID and a button to delete the name. Is there an option to place an X button on each line against each added name to delete the name without manually adding ID and pressing a button ?
My xaml code:
<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>
My xaml.cs look like:
private async void BtnDelete_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtPersonId.Text))
{
//Get Person
var person = await App.SQLiteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
if (person != null)
{
//Delete Person
await App.SQLiteDb.DeleteItemAsync(person);
txtPersonId.Text = string.Empty;
await DisplayAlert("Success", "Person Deleted", "OK");
//Get All Persons
var personList = await App.SQLiteDb.GetItemsAsync();
if (personList != null)
{
lstPersons.ItemsSource = personList;
}
}
}
else
{
await DisplayAlert("Required", "Please Enter PersonID", "OK");
}
}