Removing an Element From a Border

Nathan Sokalski 4,121 Reputation points
2020-07-01T22:58:26.28+00:00

I have an element inside a Border, and want to move that element to another location. However, I first need to remove it from the Border. Panel controls (such as Grid & StackPanel) have a Children property on which I would call the Remove method, but because Border is not a Panel, I cannot do this. What can I do to remove the element from the Border so that I can use it somewhere else (such as adding it to a Grid or StackPanel)? Thanks.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-07-02T02:04:09.473+00:00

    Hello,

    Welcome to Microsoft Q&A.

    Border has Child property, if you set it to null, you can also remove child element.

    var rect = TestRect;
    // remove child here
    TestBorder.Child = null;
    // add to another panel
    TestGrid.Children.Add(TestRect);
    

    Thanks.

    0 comments No comments

  2. Sabrina Cosolo 126 Reputation points
    2020-07-02T09:11:56.353+00:00

    Usually when I use a Border I put inside it a Grid or a StackPanel and this reduces your problem to make the same action you do with any other Grid or StackPanel.

    <Border >
    <Grid x:Name="Container">
    <!--Here you can add all children and then remove them-->

    </Grid>       
    

    </Border>

    0 comments No comments