How to get TabItems in a TabControl that has an ItemsSource element associated with a list?

I have a TabControl that has a tab related to List:

<TabControl ItemsSource="{Binding SomeList}" /> 

How to find TabItem instances? I found other answers that suggest looking at the TabControl.Items list, but it is full of Foos. Any idea?

+4
source share
3 answers

This question is asked quite often, and the answer is always this: do not do it.

In theory, you don't need an instance of TabItem , because you have to bind everything you need to change. (Also theoretically you can get an instance using ItemContainerGenerator )

+6
source

If, for example, you need to get the actual TabItem associated with the SelectedItem (which is a related object), you can use ItemContainerGenerator as mentioned by HB

 var tabItem = this.ItemContainerGenerator.ContainerFromItem(selectedObject); 
+7
source

A long time ago, I had a similar problem with treeview in wpf. I solved this with ItemContainerGenerator . If you want, you can take a look at my solution, perhaps this will help you solve your problem: How to select a TreeViewItem data file?

But I think that HB is correct with his expression: "[..] you don’t need an instance of TabItem because you have to bind everything you need [..]"

+1
source

Source: https://habr.com/ru/post/1400453/


All Articles