i want to get a parameter with button in xaml

bilel miled 21 Reputation points
2021-02-23T14:10:18.153+00:00

i want to delete or update rows of a list view by the primary key which is Label

<Label Grid.Column="1"
               Grid.Row="0"
               Text="{Binding Label}"
               FontSize="20"
               TextColor="Black"
               Margin="0,10"
               FontFamily="Comfortaa-Light"/>

 <Label Grid.Column="2"
               Grid.Row="1"
               Text="{Binding Todo}" 
               FontSize="20"
               TextColor="Black"
               Margin="0,10,0,0"
               FontFamily="Comfortaa-Light"
               HorizontalOptions="Start"
               VerticalOptions="Center"/>
 <ImageButton Grid.Column="2"
                       Grid.Row="0"
                       HorizontalOptions="End"
                       VerticalOptions="End"
                       Source="plus.png"
                       Margin="9"
                       BackgroundColor="Transparent"
                          Clicked="AddReminder"
                here parameter{{Binding Label}}                  />
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-02-23T15:20:47.517+00:00

    If you want to use a parameter, use the Command and CommandParameter properties, not the Clicked event.


  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,936 Reputation points Microsoft Vendor
    2021-02-24T02:38:45.047+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    i want to delete or update rows of a list view by the primary key which is Label.

    For testing, I change the Label to Entry. When I click the plus button, the value could be added or updated(if the primary key is not null, PS: I set an ID as primary key). You mentioned that listview in your project. So here is my layout. I wrote two commands for updating and deleting, there are different command ways(Command in listview or outside the listview is different), it depends on you choose.

       <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                    x:Class="ListviewUpdate.MainPage">  
         
           <StackLayout>  
               <Grid>  
                   <Entry Grid.Column="1"  
                       Grid.Row="0"  
                       Text="{Binding ChangeID}"  
                       FontSize="20"  
                       TextColor="Black"  
                       
                      />  
         
                   <Entry   
                       Text="{Binding Todo}"   
                       FontSize="20"  
                       TextColor="Black"  
                       Margin="0,10,0,0"  
                       HorizontalOptions="Fill"  
                       VerticalOptions="Fill"/>  
                   <ImageButton Grid.Column="2"  
                                Grid.Row="0"  
                                WidthRequest="50"  
                                HeightRequest="50"  
                                HorizontalOptions="End"  
                                VerticalOptions="End"  
                                Source="plus.png"  
                                Margin="9"  
                                BackgroundColor="Transparent"  
                                Command="{Binding UpdateCommand}"  
                                 
                             />  
         
            
         
               </Grid>  
         
               <ListView x:Name="mylistview"   
                         ItemsSource="{Binding Persons}"     
                         CachingStrategy="RecycleElement"   
                         SeparatorVisibility="None"   
                         HasUnevenRows="True"                  
                         >  
                   <ListView.ItemTemplate>  
                       <DataTemplate >  
         
                           <ViewCell >  
         
                               <Frame IsClippedToBounds="False"   
                                                      Margin="0,5"   
                                                      CornerRadius="10"   
                                                      Padding="0"   
                                                      HasShadow="False"   
                                                      BorderColor="#f0f0f0"   
                                                      BackgroundColor="Transparent" >  
                                   
         
                                   <StackLayout Padding="0" Spacing="0" MinimumHeightRequest="0">  
                                       <StackLayout Orientation="Horizontal">  
                                           <Grid>  
                                               <Label Text="{Binding Id}" Grid.Column="0" Margin="13,6,0,12"/>  
                                               <Label Text="{Binding Name}"  Grid.Column="1" Margin="13,12,5,0" FontAttributes="Bold" FontSize="18" TextColor="#222222"/>  
                                               <Button Text="Delete" Grid.Column="2" Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference Name=mylistview}}"    
                                              CommandParameter="{Binding .}"></Button>  
         
                                           </Grid>  
                                       </StackLayout>  
                                   </StackLayout>  
                               </Frame>  
                           </ViewCell>  
         
                       </DataTemplate>  
         
                   </ListView.ItemTemplate>  
               </ListView>  
           </StackLayout>  
         
       </ContentPage>  
    

    Here is layout's background code.

       public partial class MainPage : ContentPage  
           {  
               public MainPage()  
               {  
                   InitializeComponent();  
                   this.BindingContext = new PersonViewModel();  
               }  
           }  
    

    Here is my viewModel.

       using System.Collections.ObjectModel;  
       using System.ComponentModel;  
       using System.Linq;  
       using System.Text;  
       using System.Windows.Input;  
       using Xamarin.Forms;  
         
       namespace ListviewUpdate  
       {  
           public class PersonViewModel: INotifyPropertyChanged  
           {  
         
               int _changeID;  
               public int ChangeID  
               {  
                   get  
                   {  
                       return _changeID;  
                   }  
         
                   set  
                   {  
                       if (_changeID != value)  
                       {  
                           _changeID = value;  
                           OnPropertyChanged("ChangeID");  
         
                       }  
                   }  
         
               }  
         
         
               string todo;  
               public string Todo  
               {  
                   set  
                   {  
                       if (todo != value)  
                       {  
                           todo = value;  
                           OnPropertyChanged("Todo");  
         
                       }  
                   }  
                   get  
                   {  
                       return todo;  
                   }  
               }  
         
               public ObservableCollection<Person> Persons { get; set; }  
               public ICommand DeleteCommand { protected set; get; }  
               public ICommand UpdateCommand { protected set; get; }  
               public PersonViewModel()  
               {  
                   Persons = new ObservableCollection<Person>();  
                   Persons.Add(new Person() { Name = "Leon1", Image = "favourite.png",Id=1 });  
                   Persons.Add(new Person() { Name = "Leon2", Image = "unfavourite.png", Id = 2 });  
                   Persons.Add(new Person() { Name = "Leon3", Image = "favourite.png", Id = 3 });  
                   Persons.Add(new Person() { Name = "Leon4", Image = "favourite.png", Id = 4 });  
                   Persons.Add(new Person() { Name = "Leon5", Image = "unfavourite.png", Id = 5 });  
         
                   DeleteCommand = new Command<Person>(async (args) => {  
                       Person deletePerson= args as Person;  
                       Persons.Remove(deletePerson);  
                   });  
         
         
                   UpdateCommand = new Command(async () => {  
                       if (!string.IsNullOrEmpty(Todo))  
                       {  
         
                           Person item = Persons.FirstOrDefault<Person>(i => i.Id == ChangeID);  
                           if (item != null)  
                           {  
                               item.Name = Todo;  
                           }  
                           else  
                           {  
                               Persons.Add(new Person() { Id = ChangeID, Name = Todo });  
         
                           }  
                            
                       }  
                        
                   });  
         
               }  
         
               public event PropertyChangedEventHandler PropertyChanged;  
         
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
         
           }  
    

    Here is model. I achieved INotifyPropertyChanged for updating property dynamically.

       public class Person : INotifyPropertyChanged  
           {  
         
               int _id ;  
               public int Id  
               {  
                   get  
                   {  
                       return _id;  
                   }  
         
                   set  
                   {  
                       if (_id != value)  
                       {  
                           _id = value;  
                           OnPropertyChanged("Id");  
         
                       }  
                   }  
         
               }  
         
               string name;  
               public string Name  
               {  
                   set  
                   {  
                       if (name != value)  
                       {  
                           name = value;  
                           OnPropertyChanged("Name");  
         
                       }  
                   }  
                   get  
                   {  
                       return name;  
                   }  
               }  
         
                
               string image;  
               public string Image  
               {  
                   set  
                   {  
                       if (image != value)  
                       {  
                           image = value;  
                           OnPropertyChanged("Image");  
         
                       }  
                   }  
                   get  
                   {  
                       return image;  
                   }  
               }  
         
         
               public event PropertyChangedEventHandler PropertyChanged;  
         
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
         
           }  
    

    Here is running screenshot.

    71326-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. bilel miled 21 Reputation points
    2021-03-01T11:53:26.93+00:00

    Thank you for your answer ,But i tried it several times and it didn't work ,i'm really confused i did this .

     public class Pins : INotifyPropertyChanged
        {
    
            [PrimaryKey,MaxLength(250), Unique]
            public string Label { get; set; }
    
            [MaxLength(250)]
            public string Todo { get; set; }
    
            [Unique]
            public double Positionlat { get; set; }
            [Unique]
            public double Positionlon { get; set; }
            public ICommand UpdateCommand { protected set; get; }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
        }
    

    ///////////////////////////////////////////////////////////////////////////////////////////

     public partial class listofinterest : INotifyPropertyChanged
        {
            public List<Pins> list;
            public ListView lv;
            public ICommand UpdateCommand { protected set; get; }
    
            public listofinterest()
            {
                InitializeComponent();
    
                list = App.pinsRepo.GetAllPins();
    
    
                listViewEvent.ItemsSource = list;
    
                UpdateCommand = new Command<string>(async (args) => {
                    String Label = args as string;
                    _ = Navigation.PushAsync(new AddReminder(Label));
                });
    
            }
    
    
        }
    

    /////////////////////////////////////////////////////////////////////////////

                                            <ImageButton Grid.Column="2"
    
                               BackgroundColor="Transparent"
                                   Command="{Binding Path=BindingContext.UpdateCommand, Source={x:Reference Name=listViewEvent}}"  
                                           CommandParameter="{Binding Label}"
                                                         />
    

    it's not woring and even the INotifyPropertyChanged isn't working when i add something in sqlite it isn't updating property .