Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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?

like image 710
Jake Pearson Avatar asked Mar 08 '12 20:03

Jake Pearson


3 Answers

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);
like image 177
Oliver Avatar answered Nov 17 '22 14:11

Oliver


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)

like image 32
H.B. Avatar answered Nov 17 '22 14:11

H.B.


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 [..] "

like image 21
jwillmer Avatar answered Nov 17 '22 14:11

jwillmer