Pregunta

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

¿Fue útil?

Solución

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top