mercredi 16 mars 2022

When Checkbox true I want to delete item from ObservableCollection

I'm trying to implement a simple reminder list on my application. The only problem I'm facing is how to delete the item from my collection/view. I have a checkbox with each DataTemplate, what I would like to happen is when you click the checkbox it will delete the completed event from the view, i.e. the task has been completed. I know this sounds simple but I'm actually lost.

My XMAL:

<CollectionView HeightRequest="{Binding AddNotesHeight}" ItemsSource="{Binding CurrentPetEvents}">
                         <CollectionView.Header>
                             <StackLayout>
                                 <Label
                                     FontFamily="Bold"
                                     FontSize="Large"
                                     Text="+ Add Notes"
                                     TextDecorations="Underline">
                                     <Label.GestureRecognizers>
                                         <TapGestureRecognizer Tapped="AddNotes_Tapped" />
                                     </Label.GestureRecognizers>
                                 </Label>
                             </StackLayout>
                         </CollectionView.Header>
                         <CollectionView.ItemTemplate>
                             <DataTemplate>
                                 <StackLayout>
                                     <Grid>
                                         <CheckBox Color="#63a9ef"/>
                                         <Label
                                             Margin="40,0,0,0"
                                             FontSize="22"
                                             Text="{Binding TitleNote}"
                                             VerticalOptions="Center" />
                                     </Grid>
                                     <Label
                                         Margin="40,0,0,0"
                                         FontSize="20"
                                         Text="{Binding DateTimeRepeat}" />
                                     <BoxView HeightRequest="1" Color="Black" />
                                 </StackLayout>
                             </DataTemplate>
                         </CollectionView.ItemTemplate>
                     </CollectionView>

My Model:

 public class PawJournalEventModel
 {
     public string TitleNote { get; set; }
     public string DateTimeRepeat { get; set; }
 }

My ViewModel:

public ObservableCollection<PawJournalEventModel> _currentPetEvents;
         public ObservableCollection<PawJournalEventModel> CurrentPetEvents
         {
             get => _currentPetEvents;
             set
             {
                 {
                     _currentPetEvents = value;
                     OnPropertyChanged(nameof(CurrentPetEvents));
                 }
             }
         }
    
         public JournalViewModel()
         {
             CurrentPetEvents = new ObservableCollection<PawJournalEventModel>();
         }
    
         private void CreateJournalEvents()
         {
             if (CurrentItem != null)
             {
                 PetNameSelected = CurrentItem.PetName;
                 CurrentPetEvents.Clear();
                 List<PawJournalEventModel> pawJournalEventModels = CurrentItem.PawJournalEvents.ToList();
    
                 foreach (var Eventitems in pawJournalEventModels)
                 {
                     if(Eventitems.Title != "")
                     {
                         CurrentPetEvents.Add(new PawJournalEventModel()
                         {                      
                             TitleNote = Eventitems.Title + "- " + Eventitems.Notes,
                             DateTimeRepeat = Eventitems.EventDate + " " + Eventitems.EventTime + ", " + Eventitems.RepeatValue
                         });
                     }
                 }
             }
         }



Aucun commentaire:

Enregistrer un commentaire