简体   繁体   中英

How to wrap a button click event to the right ViewCell

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;

  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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM