Search code examples
c#xamlxamarinxamarin.formsstacklayout

How to get the stacklayout that contains a button when the button is clicked?


Im using a stacklayout that contains some other elements and a button.

<StackLayout x:Name="layout">
   <Button Text="Button" Clicked="Action"/>
<StackLayout/>

Is there a way to get the StackLayout that contains the button when it is clicked?


Solution

  • Use Parent property

    void Action(object sender, EventArgs args)
    {
         var button = sender as Button;
         var stackLayout = button.Parent as StackLayout;
         stackLayout.Children.Remove(button); 
    }