I'm trying to delete a doc from firebase data base on click button Img but i don't know how can I send the id of the item into delete service
this is my view file :
<ContentPage.Behaviors>
<xct:EventToCommandBehavior Command="{Binding Appearing}" EventName="Appearing" />
</ContentPage.Behaviors>
<StackLayout>
<Grid HorizontalOptions="CenterAndExpand" >
<Image Source="https://bcckids.org/wp-content/uploads/2016/10/Photo_Main_Slider2.png"
Scale="1.0"
Aspect="Fill"
/>
</Grid>
<ListView
HasUnevenRows="True"
ItemsSource="{Binding Patients}"
SelectedItem="{Binding SelectedPatient, Mode=TwoWay}"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowDefinitions="Auto,Auto,Auto">
<Frame Style="{StaticResource FrameStyle}"
Margin="15,0,30,0"
>
<StackLayout Orientation="Horizontal" Margin="10,0,20,0" VerticalOptions="Center" HorizontalOptions="Center">
<Image Source="https://www.freeiconspng.com/thumbs/profile-icon-png/profile-icon-9.png" WidthRequest="70" HeightRequest="70"/>
<BoxView
Color="Maroon"
WidthRequest="1"
Margin="5, 0, 10, 0" />
<Label Grid.Row="0" Text="{Binding PatientName}" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource labelView}"/>
<ImageButton Style="{StaticResource DeleteButton}"/>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Style="{StaticResource BlusButton}" Clicked="Button_Clicked_1" />
</StackLayout>
and How can i wrote it in viewmodel and call thw service ? this is my delete service :
public async Task DeletePatient(string ID)
{
await firebaseClient
.Child($"Specalists/406707265/Patients/{ID}")
.DeleteAsync();
}
CodePudding user response:
Code might look something like this:
// Your item model.
public class Patient
{
public string Id { get; set; }
public string PatientName { get; set; }
}
// ... your viewmodel class
{
public ObservableCollection<Patient> Patients { get; set; } = new ObservableCollection<Patient();
}
// YourView.xaml
...
<ListView.ItemTemplate>
...
<ImageButton Style="{StaticResource DeleteButton}" Clicked="ItemImageButton_Clicked" CommandParameter="{Binding .}"/>
// your view's code behind (.xaml.cs):
public partial class YourView ...
{
private async void ItemImageButton_Clicked(object sender, EventArgs e)
{
var patient = (Patient)((ImageButton)sender).CommandParameter;
await DeletePatient(patient.Id);
}
In the .._Clicked
method, sender
is the ImageButton
that was clicked on.
In xaml, CommandParameter="{Binding .}"
passes the model item (Patient
) to that method.
OR if DeletePatient is in YourViewModel, which is YourView's BindingContext:
// your view's code behind (.xaml.cs):
...
private async void ItemImageButton_Clicked(object sender, EventArgs e)
{
var patient = (Patient)((ImageButton)sender).CommandParameter;
var vm = (YourViewModel)BindingContext;
await vm.DeletePatient(patient.Id);
}