Hi,
I want to close a TabItem from the TabItem i am currently in.
For example say, there is a button in Tab Header 2. On click of that button i want to close the Tab Header 2 tab and go to Tab Header 1.
Can you tell me how to do this?
Thanks & Regards,
Varun R
As Alex mentioned in a reply to your previous posting about this, there are several commands defined for closing a pane. So if you have a TabItemEx instance that you want to close, you can invoke its ExecuteCommand method with that command (e.g. tabItem.ExecuteCommand(TabItemExCommands.Close) ). The default template for TabItemEx contains a button that uses this command and targets the TabItemEx instance.
If you want to try and control what tab gets focus after the tab is closed then you could handle the TabItemEx' Closed event and then set the SelectedItem of the tab control to a different tab item (or set the SelectedIndex to a different visible tab).
if it is in the same user control then it will work.
But, my case is diferent. The main tab is in one user control 1 and dynamically i create another tab whick contains another user control 2. On click of Save button in User control 2, i need to close the current tab and go to my main tab.
I'm sorry but I'm not sure how this is different. Regardless of where you are doing it, if you want to close the tab you would get a reference to the TabItemEx (e.g. using (Visual|Logical)TreeHelper to walk up the parent chain) and then use the Close method or invoke the TabItemExCommands.Close command. If you still have an issue then please provide a small sample that demonstrates the issue you are having.
Got it dude :)
used ((TabItemEx)TabControl.SelectedItem).ExecuteCommand(TabItemExCommands.Close);
Many thanks for the help :)
I was able to get the parent Tab Control using Visual Tree Helper :-)
var TabControl = VisualTreeHelperExtensions.FindAncestor<TabControl>(this);
public static class VisualTreeHelperExtensions { public static T FindAncestor<T>(DependencyObject dependencyObject) where T : class { DependencyObject target = dependencyObject; do { target = VisualTreeHelper.GetParent(target); } while (target != null && !(target is T)); return target as T; } }
TabControl has all the TabItemEx.
Can you tell me how invoke the TabItemExCommands.Close command?