Please critique this method
- by Jakob
Hi I've been looking around the net for some tab button close functionality, but all those solutions had some complicated eventhandler, and i wanted to try and keep it simple, but I might have broken good code ethics doing so, so please review this method and tell me what is wrong.
public void AddCloseItem(string header, object content){
//Create tabitem with header and content
StackPanel headerPanel = new StackPanel() { Orientation = Orientation.Horizontal, Height = 14};
headerPanel.Children.Add(new TextBlock() { Text = header });
Button closeBtn = new Button() { Content = new Image() { Source = new BitmapImage(new Uri("images/cross.png", UriKind.Relative)) }, Margin = new Thickness() { Left = 10 } };
headerPanel.Children.Add(closeBtn);
TabItem newTabItem = new TabItem() { Header = headerPanel, Content = content };
//Add close button functionality
closeBtn.Tag = newTabItem;
closeBtn.Click += new RoutedEventHandler(closeBtn_Click);
//Add item to list
this.Add(newTabItem);
}
void closeBtn_Click(object sender, RoutedEventArgs e)
{
this.Remove((TabItem)((Button)sender).Tag);
}
So what I'm doing is storing the tabitem in the btn.Tag property, and then when the button is clicked i just remove the tabitem from my observablecollection, and the UI is updated appropriately.
Am I using too much memory saving the tabitem to the Tag property?