Please critique this method

Posted by Jakob on Stack Overflow See other posts from Stack Overflow or by Jakob
Published on 2010-05-23T09:18:58Z Indexed on 2010/05/23 9:30 UTC
Read the original article Hit count: 298

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?

© Stack Overflow or respective owner

Related posts about tabcontrol

Related posts about observablecollection