locked
C++.In Code, Dynamically Remove Control From Xaml Tree?

    Question

  • Another newbee question. I am sure I am off on the wrong track.

    Is it not possible to remove a control from the Xaml tree in code? Basically I have a Grid Control Named "MyGrid' In My Xaml. When The User clicks on a button I create a new Grid Control in Code. I want to completely replace the existing "MyGrid" with the new Grid I created.The new Grid I Created has the same name ("MyGrid"). I of course get an exception that there is already a Control named "MyGrid" in the current tree.

    Is it possible within code to remove "MyGrid" completely from the tree? Though the help docs on VisualTreeHelper are sketchy, it seems clear that it's intended use is not what I am looking for (VisualTreeHelper::DisconnectChildrenRecursive() does not produce an exception, but the 'MyGrid' is still in the tree).

    Thanks in advance.

    VisualTreeHelper

    Thanks.

    Thursday, April 11, 2013 7:29 PM

Answers

  • To remove a control from Xaml you need to remove it from its parent. If the parent is some form of content control (i.e. it can only have a single child element) then you would remove it by setting the parent's Content property to nullptr (or to the new element you want to replace it with). If the parent can have multiple children, then it will have a property called Children which is an IVector<UIElement^> interface pointer. You would iterate through the children until you found the child you wanted to remove and then you would remove it using one of the Remove* functions such as RemoveAt(uint index).

    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    • Marked as answer by RSullivan59 Monday, April 15, 2013 5:10 PM
    Friday, April 12, 2013 10:31 AM

All replies

  • To remove a control from Xaml you need to remove it from its parent. If the parent is some form of content control (i.e. it can only have a single child element) then you would remove it by setting the parent's Content property to nullptr (or to the new element you want to replace it with). If the parent can have multiple children, then it will have a property called Children which is an IVector<UIElement^> interface pointer. You would iterate through the children until you found the child you wanted to remove and then you would remove it using one of the Remove* functions such as RemoveAt(uint index).

    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    • Marked as answer by RSullivan59 Monday, April 15, 2013 5:10 PM
    Friday, April 12, 2013 10:31 AM
  • Hi,

     

    Maybe you can try the function to traversal visual tree.

    Windows::UI::Xaml::FrameworkElement^ FindVisualChildByName(DependencyObject^ obj, String^ name)
    {
    	FrameworkElement^ ret ;
    	int numChildren = VisualTreeHelper::GetChildrenCount(obj);
    	 for (int i = 0; i < numChildren; i++)
                {
                    auto objChild = VisualTreeHelper::GetChild(obj, i);
                    auto child = safe_cast<FrameworkElement^>(objChild);
    				if (child != nullptr && child->Name == name)
                    {
                        return child;
                    }
    
                    ret = FindVisualChildByName(objChild, name);
    				if (ret != nullptr)
                        break;
                }
                return ret;
    }

     

    Best regards,

    Jesse



    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 15, 2013 2:37 AM
  • Thanks Mike, Jesse.

    Just to make sure, RemoveAt(Index) deletes, kills, removes, deletes resources,,... of the UiElement at Index? It is completely destroyed and there are no remnants of it left? This happens immediately?

    Also: Panel->Child=nullptr; If the Child is a grid with multiple rows & columns with lots of UI element in the rows and columns all of the UI elements contained within the grid (Grid->Children) will also be completely destroyed? This happens immediately?

    Thanks again.

    Monday, April 15, 2013 5:10 PM
  • The UIElement is a reference counted object. If there are no references left, it releases all resources. If you create a situation in which there's a circular reference, then its resources wouldn't be freed but would live on until the circular reference was broken or the program terminated (whichever happens first).

    If the Child is a Grid (or anything else that can have child elements) then when the Child is destroyed the reference(s) it holds to its child/children will be nulled out, causing its/their reference count(s) to be decremented. Just like in the paragraph above, if it's the last reference then the object is destroyed and its resources are freed. But again, if there's a circular reference or some other reference then the object would not be destroyed until there are no remaining references to it.

    Anything that is destroyed should have all resources released immediately. There is no GC; destruction is deterministic.


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Monday, April 15, 2013 9:07 PM