如何将按钮单击事件包装到右侧ViewCell

时间:2015-06-17 12:42:12

标签: c# xamarin viewmodel xamarin.forms custom-cell

我已经创建了一个CustomCell并在其上放了一个按钮。

http://i1068.photobucket.com/albums/u451/tasknick/Captura%20de%20Tela%202015-06-15%20as%2010.05.10_zpsit980vqh.png

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

来自按钮的点击事件有效。但我怎么知道这个事件来自哪个细胞? 例如:当我点击第一个单元格上的按钮时,我想显示第一个单元格中的文本;

2 个答案:

答案 0 :(得分:1)

  1. 为什么处理ActionButton.Clicked而不是处理Command? 你已在这里定义了绑定

    ActionButton.SetBinding(Button.CommandProperty,“commandActionButton”,BindingMode.TwoWay);

  2. 因此,您可以处理view-model中的点击。

    1. 如果您想处理此事件,sender应该是您按钮的实例。

答案 1 :(得分:0)

尝试使用FrameworkElement.Parent Property并将其转换为自定义单元格。这将为您提供按住按钮的单元格的句柄。一个粗略的例子是“(CustomCell)sender.Parent”