locked
How to get TabItem from HeaderTemplate? RRS feed

  • Question

  • My TabItem that has custom HeaderTemplate. HeaderTemplate contains UserControl with one button. Now when the button clicked, who do I obtain the TabItem in button click event? (So I can do stuff with it). I have tried to use VisualTreeHelper GetParent() method to retrieve TabItem but no luck

    TabItem->TabItem.HeaderTemplate->MyUserControl->Button

    Tuesday, June 5, 2012 2:17 PM

Answers

  • One way is to traverse the visual tree to find the parent, you may do this using VisualTreeHelper.  Another way is to use Reflection on the Button until you find the type of TabItem, like this  var control = MyButton.Parent.  Keep iterating each control's parent until you find the tab item.  Another way it to put something in your Tag property of the Button identifying the name of the TabItem!

    And I'm sure there's other ways to.  For example, you can have an event handler which when the TabItem is selected, a variable named CurrentTabItem is set! No reflection needed!


    JP Cowboy Coders Unite!

    • Marked as answer by GaiusBaltar Wednesday, June 6, 2012 6:51 AM
    Tuesday, June 5, 2012 5:48 PM