如何在Xamarin中实现ListView的DataTemplate的子项的方法。表格
问题描述:
我有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 };
});
现在的问题是,我要实现对图像自来水和按钮单击事件的按钮控制点触手势。这个怎么做?
答
一个按钮
btnDelete.Clicked += ((sender, args) => {
// perform actions here
});
点击处理程序附加手势识别到图像
TapGestureRecognizer tapped = new TapGestureRecognizer();
tapped.Tapped += ((o2, e2) =>
{
// perform actions here
});
img.GestureRecognizers.Add(tapped);
你可以考虑使用'ContextActions'你'DataTemplate'如果是有意义的,你的应用程序,因为这是一个非常常见的交互,并在每个平台上以本地方式工作。与试图在可点击的ListView行中点击按钮相比,ContextActions在小屏幕上工作起来要容易得多。查看'ContextActions' [here](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions) – hvaughan3