Reputation: 27757
I have a TabControl that has tab bound to a List:
<TabControl ItemsSource="{Binding SomeList}" />
How can find the instances of TabItem? I found other answers that suggest looking at the TabControl.Items list but that is full of Foos. Any idea?
Upvotes: 6
Views: 7803
Reputation: 36453
If, for example, you need to get the actual TabItem related to the SelectedItem (which is a bound object) you can use the ItemContainerGenerator
as H.B mentioned
var tabItem = this.ItemContainerGenerator.ContainerFromItem(selectedObject);
Upvotes: 13
Reputation: 3789
Long time ago I had a similar problem with the treeview in wpf. I have solved it using ItemContainerGenerator
. If you want you can have a look at my solution, maybe it helps you with your problem: How to select a databound TreeViewItem?
But i think H.B. is right with his statement: " [..] you should not need the TabItem instance because you should bind everything you need [..] "
Upvotes: 1
Reputation: 185290
That question gets asked pretty often and the answer always is: Don't do it.
In theory you should not need the TabItem
instance because you should bind everything you need to modify. (Also in theory you could get the instance using the ItemContainerGenerator
)
Upvotes: 6