简体   繁体   中英

How to implement method of sub item of DataTemplate of ListView in Xamarin.Forms

I have ListView and I assigned DataTemplate to it. Now I have some controls in it like Button, Images and Labels. Now I want some actions on click event of that controls.

My ListView is like this-

listof_ingredients_and_lifestyleDiets = new ListView
{
 BackgroundColor = Color.FromHex ("#CDBA96"),
 ItemTemplate = preferenceTableCell,
 SeparatorColor =Color.FromHex("#CDBA96"),
 RowHeight=40,
 HeightRequest=200
};

My DataTemplate is like this-

    DataTemplate preferenceTableCell = new DataTemplate (() => {
                var itemName = new Label {              
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    WidthRequest=80,
                    FontSize=14,
                    TextColor=ColorResources.TextColor,
                };
                itemName.SetBinding (Label.TextProperty, "Name");


                var radiobtn_allergen = new CircleImage {
                    BorderColor = ColorResources.commonButtonBackgroundColor,
                    HeightRequest = 25,
                    WidthRequest = 25,
                    Aspect = Aspect.AspectFill,
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    Source="radio_Check.png"
                };
radiobtn_allergen.SetBinding(CircleImage.IsVisibleProperty,"isExcluded");

                var radiobtn_preference = new CircleImage {
                    BorderColor = ColorResources.commonButtonBackgroundColor,
                    HeightRequest = 25,
                    WidthRequest = 25,
                    Aspect = Aspect.AspectFill,
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    Source="radio_uncheck.png",
                };radiobtn_preference.SetBinding (CircleImage.IsVisibleProperty, "isExcluded");

                var btnDelete=new Button{
                    Image="deleteBtn.png",
                    HorizontalOptions=LayoutOptions.EndAndExpand,
                    HeightRequest=50,
                    WidthRequest=30
                 };

                StackLayout stacklayout = new StackLayout {
                    Spacing = 60,
                    Orientation = StackOrientation.Horizontal,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Children = { itemName,radiobtn_allergen,radiobtn_preference,btnDelete }
                };
                return new ViewCell { View = stacklayout };
            });

Now My question is, I want implement tap gesture for image tap and button click event for button control. How to do this?

Click handler for a button

btnDelete.Clicked += ((sender, args) => {
      // perform actions here
  });

attach a gesture recognizer to an Image

TapGestureRecognizer tapped = new TapGestureRecognizer();
tapped.Tapped += ((o2, e2) =>
    {
        // perform actions here
    });

img.GestureRecognizers.Add(tapped);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM