Frage

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

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top