Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting when a tab item was added to WPF Tab control

Tags:

c#

wpf

I am trying to synchronize the selected tab item of a WPF tab control with the last item that was added.

Since there is no such property as e.g. IsSynchedWithLastAddedItem, I am trying to detect when an item was added in order to point the SelectedItem at the last added one.

I cannot find the event that gets raised - either on the tab control or its Items, when a TabItem was added.

I am sure something like it must exist though, so I hope someone can help me out.

like image 414
Thorsten Lorenz Avatar asked Sep 03 '10 22:09

Thorsten Lorenz


1 Answers

var view=CollectionViewSource.GetDefaultView(m_tabControl.ItemsSource);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};

If you work directly with the Items-collection, the same technique will work also. Get the default CollectionViewSource for this collection.

var view=CollectionViewSource.GetDefaultView(m_tabControl.Items);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};

As Timores wrote, for the m_tabControl.Items-property, you can attach a handler directly. The same is also true for most ItemsSource-references, but there you have to check yourself for the INotifyCollectionChanged-interface.

I have not tested it. Make a comment if it does not work.

like image 187
HCL Avatar answered Sep 20 '22 10:09

HCL