0
votes

hello i want to pass the Label with the button in xaml its a list view and each row has her own Label and i want update each row by the label

    <Label Grid.Column="2"
                                             Grid.Row="1"
                                             Text="{Binding Label}" 
                                             FontSize="20"
                                             TextColor="Black"
                                             Margin="0,10,0,0"
                                             FontFamily="{StaticResource font}"
                                             HorizontalOptions="Start"
                                             VerticalOptions="Center"/>

                                    <ImageButton Grid.Column="2"
                                             Grid.Row="0"
                                             HorizontalOptions="End"
                                             VerticalOptions="End"
                                             Source="plus.png"
                                             Margin="9"
                                            clicked ="update"
                                            BackgroundColor="Transparent"/>

and the function in the class

public void update (object sender , EventArgs e) { update by the Label }

i know that's there is something with commands but i'm beginner and i don't know how to use it i tried this and i didn't work.

Command="{Binding Path=BindingContext.UpdateCommand, Source={x:Reference Name=listViewEvent}}"  
                                   CommandParameter="{Binding Label}"

and the function is this

        UpdateCommand = new Command<string>(async (args) => {
            String Label = args as string;
             DisplayAlert("update", "update", "OK");
            _ = Navigation.PushAsync(new AddReminder(Label));
        });

and the alert didn't show please i need help

2
Do you want to update the value of label's text when click the button ? - Lucas Zhang
no i want to update the object in the sqlite by the label - Miled Bilel
We could add some properties to model and binding them to the label. In this way we could get the properties in ViewModel . It is not a good design to pass the element in MVVM . - Lucas Zhang

2 Answers

0
votes

you don't need to "pass" the Label

<ImageButton Grid.Column="2" Cilcked="update" ... />

protected void update(object sender, EventArgs a)
{
   var btn = (ImageButton)sender;

   // MyClassName should be the name of your model class
   // this will get you the object referenced by the selected row
   var item = (MyClassName)btn.BindingContext;
   // then you can reference any property of item
   // ie, item.Label, etc

}
0
votes
<Label x:Name="myLabel"
       Grid.Column="2"
       Grid.Row="1"
       Text="{Binding Label}" 
       FontSize="20"
       TextColor="Black"
       Margin="0,10,0,0"
       FontFamily="{StaticResource font}"
       HorizontalOptions="Start"
       VerticalOptions="Center"/>

<ImageButton Grid.Column="2"
             Grid.Row="0"
             HorizontalOptions="End"
             VerticalOptions="End"
             Source="plus.png"
             Margin="9"
             clicked ="update"
             BackgroundColor="Transparent"
             CommandParameter={x:Reference myLabel}/>

And then in code behind you can get the label

protected void update(object sender, EventArgs a)
{
   var btn = (ImageButton)sender;
   var label = btn.CommandParameter as Xamarin.Forms.Label;
}