XAML Button not garbage collected after eliminating references
The button is not collected because it was strongly referenced within the Window namescope:
But it shouldn't be recognized as memory leak, because you should reregister your new button within the scope:
//...INameScope scope = NameScope.GetNameScope(this);scope.UnregisterName("btn");btn = new System.Windows.Controls.Button();Content = btn;scope.RegisterName("btn", btn);//...