2016-07-22 20 views
1

我有ListView,我給它分配了DataTemplate。現在我有一些控件,如按鈕,圖像和標籤。現在我想對該控件的單擊事件進行一些操作。如何在Xamarin中實現ListView的DataTemplate的子項的方法。表格

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

現在的問題是,我要實現對圖像自來水和按鈕單擊事件的按鈕控制點觸手勢。這個怎麼做?

+0

你可以考慮使用'ContextActions'你'DataTemplate'如果是有意義的,你的應用程序,因爲這是一個非常常見的交互,並在每個平臺上以本地方式工作。與試圖在可點擊的ListView行中點擊按鈕相比,ContextActions在小屏幕上工作起來要容易得多。查看'ContextActions' [here](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions) – hvaughan3

回答

0

一個按鈕

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

點擊處理程序附加手勢識別到圖像

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

img.GestureRecognizers.Add(tapped); 
相關問題