如何在Xamarin.Forms中实现ListView的DataTemplate子项的方法

时间:2016-07-22 13:26:26

标签: c# listview xamarin.forms

我有ListView,我给它分配了DataTemplate。现在我有一些控件,如按钮,图像和标签。现在我想对该控件的click事件采取一些操作。

我的ListView就是这样 -

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

我的DataTemplate是这样的 -

    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 };
            });

现在我的问题是,我希望用于按钮控制的图像点击和按钮点击事件的实现点击手势。怎么做?

1 个答案:

答案 0 :(得分:0)

单击按钮的处理程序

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

将手势识别器附加到图像

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

img.GestureRecognizers.Add(tapped);