How to wrap a button click event to the right ViewCell

409 views Asked by At

I've created a CustomCell and puts a button on it.

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

The click event from button works. But how I know what cell this event comes? For exemple: When I click on the button on the first cell I wanna show the text from the first cell;

2

There are 2 answers

0
umbreon222 On

Try to use the FrameworkElement.Parent Property and cast it as the custom cell. This will give you a handle on the cell which holds the button. A rough example would be "(CustomCell)sender.Parent"

0
Artur Shamsutdinov On
  1. Why do you handle ActionButton.Clicked instead of handling Command? You've defined binding here

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

So you could handle click in your view-model.

  1. If you want to handle this event - sender should be your button's instance.