2016-07-22 6 views
1

ListViewがあり、DataTemplateが割り当てられています。今、ボタン、画像、ラベルのようなコントロールがいくつかあります。今私は、そのコントロールのクリックイベントでいくつかのアクションをしたい。Xamarin.FormsでListViewのDataTemplateのサブ項目のメソッドを実装する方法

this-

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

私のDataTemplateを今すぐ私の質問は、私はボタンコントロールの画像をタップし、ボタンクリックイベント用のタップジェスチャーを実装したいされ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 }; 
      }); 

のようなものであるように私のリストビューがあります。これを行う方法?

+0

にジェスチャー認識を添付これは非常に共通の相互作用であり、各プラットフォームでネイティブな方法で動作します。 'ContextActions'は、クリック可能な' ListView'行内のボタンをクリックしようとするのと比べて、小さな画面で作業する方がはるかに簡単です。 'ContextActions' [here](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions)をチェックしてください。 – hvaughan3

答えて

0
ボタン

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

ため

クリックハンドラがあるため、それはあなたのアプリケーションのために理にかなっている場合は、あなたの `DataTemplate`に` ContextActions`を使用して検討するかもしれない画像

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

img.GestureRecognizers.Add(tapped); 
関連する問題