1

This question is similar to this question I asked earlier today. The difference is, now I would like to delete a Tab Item referenced by it's name or header. Can I call Remove in a fashion similar to the answer I got on this question?

This is what I've tried:

tabControl.Items.Remove = tabControl.Items //Changes tab according to TreeView
                        .OfType<TabItem>().SingleOrDefault(n => n.Name == stringValue);

Can I use something like this? If so, how?

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79
  • 2
    I strongly suggest MVVM as the default way of thinking for WPF. winforms-like code-behind type of hacks and manipulating UI elements in procedural code will only bring you pain and suffering and torture and miserable failure. Much more so when you're dealing with `ItemsControl`-derived elements (such as TabControl or Menu or ListBox or ComboBox). – Federico Berasategui Aug 16 '13 at 17:17
  • It doesn't exactly work because `Remove` is a method group. I'll post what I tried. – Eric after dark Aug 16 '13 at 17:19
  • 1
    Remove is not a property, you cannot assign a value to it – Sylens Aug 16 '13 at 17:27
  • 1
    Really, with the down vote? – Eric after dark Aug 16 '13 at 17:47

1 Answers1

1

I don't know much about removing from wpf, however this code is way more likely to work than what you have posted. Remove is a method, you can't assign it a value, so you have to isolate the item you want to remove, check to make sure it isn't null, then pass the object into the Remove method.

var tabToDelete = tabControl.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == stringValue);
if (tabToDelete != null) // Since you chose to use SingleOrDefault, we have to check to make sure it isn't null before we try to remove it.
tabControl.Items.Remove(tabToDelete);

However, I strongly suggest you take a look at WPF - Best way to remove an item from the ItemsSource since it goes into details about checking IF the item CAN be removed, and even if the Remove method is available to that control.

Community
  • 1
  • 1
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40