dimioLt
dimioLt

Reputation: 59

How to get value of the Text property of a Label tapped when using Tapped GestureRecognizer?

I have a Label inside a CollectionView and I want to pass it as a parameter to a Tapped event. How can I do it?

I get Xamarin.Forms.Label as a parameter instead of EmojiSource.

XAML

 <CollectionView
                    x:Name="collectionView"
                    Margin="-10,-15,-10,-10"
                    HeightRequest="270"
                    VerticalScrollBarVisibility="Never">

                    <CollectionView.ItemsLayout>
                        <GridItemsLayout Orientation="Horizontal" Span="5" />
                    </CollectionView.ItemsLayout>
                    <CollectionView.ItemTemplate>

                        <DataTemplate>

                            <Label
                                x:Name="Labellabel"
                                Margin="10"
                                FontSize="30"
                                Text="{Binding EmojiSource}"
                                TextColor="#FF000000">
                                <Label.GestureRecognizers>
                                    <!-- <TapGestureRecognizer Command="{Binding Path=BindingContext.EmojiTappedCommand, Source={x:Reference EditorView}}" CommandParameter="{Binding EmojiSource}" />-->
                                    <TapGestureRecognizer CommandParameter="{Binding EmojiSource}" Tapped="TapGestureRecognizer_Tapped" />
                                </Label.GestureRecognizers>

                            </Label>


                        </DataTemplate>
                    </CollectionView.ItemTemplate>

                </CollectionView>

Code Behind

     ObservableCollection<Emojis> EmojiList = new ObservableCollection<Emojis>();
     collectionView.ItemsSource = EmojiList;

     public Control(){
    
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SlightlySmilingFace) , EmojiMethodCommand = "smiley_face" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithStuckOutTongueAndWinkingEye), EmojiMethodCommand = "upside_down_face" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.LoudlyCryingFace), EmojiMethodCommand = "fmiling_face_circle_eyes" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.WinkingFace), EmojiMethodCommand = "smiling_emoi_eyes" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithHeartEyes), EmojiMethodCommand = "smiling_emoji_smiling_eyes" });
    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e){
                var emoji = sender as string;
                EntryControl.Text = EntryControl.Text + emoji;
    }

Upvotes: 1

Views: 535

Answers (1)

deczaloth
deczaloth

Reputation: 7455

As i understand from your code

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var emoji = sender as string;
    EntryControl.Text = EntryControl.Text + emoji;
}

you should be all right by simpy using the Text property of your sender (Label):

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var emoji = ((Label)sender).Text;
    EntryControl.Text = EntryControl.Text + emoji;
}

Also note that where your write

<Label.GestureRecognizers>
    <!-- <TapGestureRecognizer Command="{Binding Path=BindingContext.EmojiTappedCommand, Source={x:Reference EditorView}}" CommandParameter="{Binding EmojiSource}" />-->
    <TapGestureRecognizer CommandParameter="{Binding EmojiSource}" Tapped="TapGestureRecognizer_Tapped" />
</Label.GestureRecognizers>

CommandParameter has no effect since it works with Command and not with the Tapped event.

Upvotes: 1

Related Questions