In WPF I have created a control that dynamically creates buttons for me. On some occasions the buttons may change and need be recreated. I am currently using the following:
public void GenerateButtons()
{
WrapPanel_Main.Children.Clear();
foreach (ActivatedItem thisItem in Controller.ItemList.Where(sl => sl.IsTypeCompatible(typeof(ActivatedItem))))
{
Button newButton = new Button() { Content = thisItem, ToolTip = thisItem.Desc, Width = 50, Height = 25 };
newButton.Click += new System.Windows.RoutedEventHandler(this.DynamicButtonClick);
WrapPanel_Main.Children.Add(newButton);
}
}
I am wondering if the WrapPanel_Main.Children.Clear();
section of my code is enough to remove the buttons and the events from memory or if I am leaving stuff (like the event handlers) floating around out there?
As always I am open to suggestions on bettering the code shown above as well.