0

I have a CustomControl with some elements and a Button. I want to remove/close/dispose the CustomControl when the Button clicked. I tried to get the parent of the element when the Button is clicked but all I get is crush.

My CustomControl is TabItem and I want to remove it from the TabControl , so to get the TabControl I use the following code (and it crushes):

TabControl parent = VisualTreeHelper.GetParent(this) as TabControl;

What am I doing wrong? Maybe my approach is wrong? If so, how can I remove/close/dispose the TabItem when the Button is clicked?

Thanks

Ron
  • 3,975
  • 17
  • 80
  • 130
  • you custom control is deriving TabItem or is a usercontrol placed inside TabItem of TabControl? – Nitin Oct 22 '13 at 05:26

1 Answers1

1

The parent of your CustomControl will still be TabItem. Try below code:

Clears the content of TabItem:

        TabItem tabItem = this.Parent as TabItem;
        tabItem.Content = null;

If you want to remove the TabItem from TabControl then:

        ((TabControl)tabItem.Parent).Items.Remove(tabItem);
Nitin
  • 18,344
  • 2
  • 36
  • 53