Loading UIElement content at runtime

Hi,
I’m sort of new to using Unity’s UI systems and I am trying to create a UI pop-up template that I could load content into at runtime. I made a mistake thinking that EditorWindow was able to pop-up in-game so I made a demo pop-up (that can’t load content at runtime) version through EditorWindow first, but now I’m trying to transition to a “Canvas-Panel pop up”. For both I used an UXML template that has all the elements needed in place, then through script load in the text that I want to display onto the pop-up window.

However, when I do that, for some reason even the close button on the pop-up ceased to work, and none of the contents are loading in either. I am starting the debugging with fixing the button. Through using Debug.Log I could tell that the RegisterCallback is called, but the button clicks aren’t registered because the following methods aren’t called.
For the button, the code I am using looks like this

rootVisualElement.Query<Button>().ForEach(RegisterHandler);
  
private void RegisterHandler(Button btn){
    Debug.Log("Handler added ");
    btn.RegisterCallback<ClickEvent>(ClosePopUp);
}
private void ClosePopUp(ClickEvent e){
    Debug.Log("Button clicked ");
    this.gameObject.SetActive(false);
}

If you guys could let me know what I could do to try to fix the button and/or the content not loading in, or if there is a better way to do this, please let me know. Thanks ahead.

Update: I tried using UI Builder’s custom controls to replace the button but it’s still not working. Not sure if I did it correctly.

public class CloseButtonController : Button
{
    // Start is called before the first frame update
    void Start()
    {
        //this.RegisterCallback<ClickEvent>(ClosePopUp);
        this.clicked += ClosePopUp;
    }
    private void ClosePopUp(/*ClickEvent e*/){
        Debug.Log("Button Clicked");
    }

    #region UXML
    [Preserve]
    public new class UxmlFactory : UxmlFactory<CloseButtonController, UxmlTraits>{}
    [Preserve]
    public new class UxmlTraits : VisualElement.UxmlTraits{}
    #endregion
}

Update #2:
Using the UI Toolkit Debugger I can see that the button click is registered because the Pseudo State used to update the button’s USS is changing to “active” whenever I click. However, the Callback event still isn’t triggering for some reason.

Fixed, but I thought once this post goes through moderation it’s probably good to still leave it for someone in the future.

Turns out, the things that you do, whether it is loading in content or adding Callback to buttons, gets reset once you disable the gameObject that contains it. So when I try to hide the pop-up with setActive(false), it resets the window and all the changes is reverted. Once I moved all the modifications to OnEnable instead of Start, the problem fixed itself.