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?
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);
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
)
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 [..] "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With