Xamarin | Delete parent stacklayout with onclick of button

24 Views Asked by At

I am trying to create a screen where the user adds players to a game. The screen starts with just one entry and a "X" button next to it. Below that is an "Add new player" button which just adds a new StackLayout with the same entry and button as before.

Now I want the "X" button to remove its parent StackLayout, in turn removing itself and the entry. I'm sure its a simple thing to do, I just can't find anything that works.

The .xaml file:

<ScrollView>
    <StackLayout>
        <Label Text="Enter Players:"></Label>
        <StackLayout x:Name="Players">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
                <Entry Placeholder="Player" WidthRequest="300"/>
                <Button Text="X" WidthRequest="45" Clicked="DeleteBtn_Clicked"></Button>
            </StackLayout>
        </StackLayout>
        <Button VerticalOptions="End" Text="Add Player" Clicked="AddBtn_Clicked"></Button>
    </StackLayout>
</ScrollView>

The .xaml.cs file:

public void AddBtn_Clicked(object sender, EventArgs args)
{
    var newplayer = new StackLayout
    {
        Orientation = StackOrientation.Horizontal,
        HorizontalOptions = LayoutOptions.Center,
        Children =
    {
        new Entry() { Placeholder = "Player", WidthRequest=300 },
        new Button() { Text = "X", WidthRequest = 45 },
    }
    };
    Players.Children.Add(newplayer);

}
public void DeleteBtn_Clicked(object sender, EventArgs args)
{
   
}

I tried a few different things from the Xamarin documentation and some other related internet forms but couldn't seem to get anything to work, so I just left DeleteBtn_Clicked empty.

0

There are 0 best solutions below