Jake Pearson
Jake Pearson

Reputation: 27757

How to get the TabItems in a TabControl that has it's ItemsSource bound to a list?

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

Answers (3)

Oliver
Oliver

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

jwillmer
jwillmer

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

brunnerh
brunnerh

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

Related Questions