Removing a specific type of child from grid control

I want to remove a control whose type has a border from the grid control. How can I achieve this in WPF C #?

Sorry guys. My problem is that I have a grid control that has a GUI at the end of XAML and a user control that are added using C #, and some controls overlap. Some controls are removed, but some of them remain superimposed. How to remove all controls. The code you posted works for control that doesn't overlap, but for overlapped ones, it doesn't work.

+3
source share
4 answers

Here is my code:

 int intTotalChildren = grdGrid.Children.Count-1;
            for (int intCounter = intTotalChildren; intCounter > 0; intCounter--)
            {
                if (grdGrid.Children[intCounter].GetType() == typeof(Border))
                {
                    Border ucCurrentChild = (Border)grdGrid.Children[intCounter];
                    grdGrid.Children.Remove(ucCurrentChild);
                }                
            }

My mistake was that every time I used Children.Countto loop for, and every time I deleted a child Children.Count, not all the children changed .

+5
source

Well, you could go through VisualTreeand delete everything that has a type Border.

static public void RemoveVisual(Visual myVisual)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
    {
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        if(childVisual.GetType() == typeof(Border))
        {
            // Remove Child
        }

        // Enumerate children of the child visual object.
        RemoveVisual(childVisual);
    }
}

The delete that I leave to you, but the above should find all the controls in a visual type view Border.

0
source

try this, grd - Grid Contol

    <Grid x:Name="grd">
    <Border x:Name="test1" Margin="5"
            Background="red"
            BorderBrush="Black"
            BorderThickness="5"></Border>
    <Button VerticalAlignment="Bottom" Content="Hello" Click="test"></Button>
</Grid> 

        for(int i=0; i< VisualTreeHelper.GetChildrenCount(grd);i++){
Visual childVisual = (Visual)VisualTreeHelper.GetChild(grd, i);
if (childVisual is Border)
{
    grd.Children.Remove((UIElement)  childVisual);
}
0
source

This is my solution to remove all border controls in a child collection of a mesh

    int indice = 0;
    int childrens = TargetGrid.Children.Count;
    for (int i = 0; i < childrens; i++)
    {
        Border brd = TargetGrid.Children[indice] as Border;
        if (brd != null)
        {
            //Remove children
            TargetGrid.Children.RemoveAt(indice);
        }
        else
        {
            indice++;
        }
    }
0
source

Source: https://habr.com/ru/post/1740415/


All Articles