我已经创建了一个CustomCell并在其上放了一个按钮。
public class CustomCell : ViewCell
{
public CustomCell ()
{
var Name = new Label {
TextColor = Color.Black,
FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
VerticalOptions = LayoutOptions.Start, HorizontalOptions = LayoutOptions.Start,
FontFamily = Device.OnPlatform ("GillSans", "Quattrocento Sans", "Comic Sans MS")
};
Name.SetBinding (Label.TextProperty, "FirstName", BindingMode.TwoWay);
var LastName = new Label {
TextColor = Color.Gray,
FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
VerticalOptions = LayoutOptions.End, HorizontalOptions = LayoutOptions.Start
, FontFamily = Device.OnPlatform ("GillSans", "Quattrocento Sans", "Comic Sans MS")
};
LastName.SetBinding (Label.TextProperty, "LastName", BindingMode.TwoWay);
var ActionButton = new Button {
Image = Images.ActionButton,
Style = Styles.DefaultButtonStyle,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.End
};
ActionButton.SetBinding (Button.CommandProperty, "commandActionButton", BindingMode.TwoWay);
StackLayout stack = new StackLayout {
Padding = new Thickness (20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = { Name, LastName, ActionButton }
}
{CODE}
View = layout;
ActionButton.Clicked += (object sender, System.EventArgs e) => Debug.WriteLine ("asdjhadjsad");
}
}
来自按钮的点击事件有效。但我怎么知道这个事件来自哪个细胞? 例如:当我点击第一个单元格上的按钮时,我想显示第一个单元格中的文本;
答案 0 :(得分:1)
为什么处理ActionButton.Clicked
而不是处理Command
?
你已在这里定义了绑定
ActionButton.SetBinding(Button.CommandProperty,“commandActionButton”,BindingMode.TwoWay);
因此,您可以处理view-model
中的点击。
sender
应该是您按钮的实例。答案 1 :(得分:0)
尝试使用FrameworkElement.Parent Property并将其转换为自定义单元格。这将为您提供按住按钮的单元格的句柄。一个粗略的例子是“(CustomCell)sender.Parent”