How to wrap a button click event to the right ViewCell

tasknicktasknick USMember ✭✭

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

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 to 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;

Answers

  • NicolasHotterbeekxNicolasHotterbeekx USMember ✭✭✭

    try this:

    ActionButton.CommandParameter = this;

    where this = the viewcell.

    That way, in your command, you will receive the ViewCell in which the button resides.

  • stvansolanostvansolano UMInsider, University ✭✭✭
    edited June 2015

    Hello @tasknick,

    I suggest you to make ActionButton, Name and LastName properties for the CustomCell class instead. Then you can handle values properly even the user changes the value (e.g Entry) and events easily:

    public class CustomCell : ViewCell
    {
        protected Label Name { get; set; }
        protected Label LastName { get; set; }    
        protected Button ActionButton { get; set; }    
    
        public CustomCell ()
        {
            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);
    
            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);
    
            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 } 
            }
    
        //..
    
            View = layout;
    
            ActionButton.Clicked += OnClicked;
        }
    
        private void OnClicked(object sender, System.EventArgs e) 
        {
            Debug.WriteLine (Name.Text);
        }
    }
    

    Hope it helps

Sign In or Register to comment.