close a TabItem

Topics: Prism v2 - WPF 3.5
Jul 29, 2009 at 3:10 PM
Hi All,
I'm trying to remove a view from a TabControl following this example:
http://blogs.infosupport.com/blogs/willemm/archive/2008/07/31/Creating-closeable-tabitems-for-use-in-CompositeWPF.aspx
I have a tabbed 'MainRegion' inside the Shell window:
<Window x:Class="Shell">
<Grid>
     <TabControl cal:RegionManager.RegionName="MainRegion" Style="{DynamicResource Shell.MainRegion}">
      </TabControl>
</Grid>
</Window>
Then add some views from some module:
_regionManager.Regions["MainRegion"].Add(myView1);
Each TabItem has a close button calling a static command (CloseTabItemCommand):
public class Commands
{
        static Commands()
        {
            CloseTabItemCommand = new DelegateCommand<TabItem>(CloseTabItem);
        }

        public static DelegateCommand<TabItem> CloseTabItemCommand { get; private set; }

        private static void CloseTabItem(TabItem tabItem)
        {
            if (tabItem != null)
            {
                FrameworkElement view = tabItem.Content as FrameworkElement;
                TabControl tabControl = TryFindParent<TabControl>(tabItem);
                var regionName = RegionManager.GetRegionName(tabControl);
                var rm = RegionManager.GetRegionManager(view);
                                
                rm.Regions[regionName].Remove(view);
            }
        }
}
The command executes perfectly, and inside this method:  'private static void CloseTabItem(TabItem tabItem)' i can get the correct tabItem, tabControl and view.
The problem is that RegionManager.GetRegionManager(view) returns the region manager that manage regions inside the scope of the view,
and i need the region manager that can remove the view from the 'MainRegion'.
Any ideas?
Thanks
Ezequiel
Jul 29, 2009 at 5:16 PM

I think that a lot of us are trying to get working prism with a module - for instance customers - that loads in a tabcontrol many times.

For instance, I'm working in a tab with customer A and I want to open in a second tab customer B.

May be someone can post a complete sample of how to get a module working like that, and of course - like Ezequiel asks - how to save / close individual tabs ?

I'm sure it will be a great step in a wider adoption of prism, I saw many discutions trying to get this behavieurs.

Thaks in advance

Marcos

Jul 29, 2009 at 5:27 PM

Hi Ezequiel,

There is not an out of the box way in Prism to get the RegionManager of the TabRegion in this kind of situation. I do not know you exact scenario, but I assume from what you explained that each of your Tabitems creates a scoped region manager for itself. I can think of two different possibilities to get the RegionManager that contains the TabReigon in this scenario.

  1. If it is the RegionManager from the entire application (as it appears to be because it is in the Shell), you could get it through the UnityContainer. Your code would look like this
    private static void CloseTabItem(TabItem tabItem)
    {
    if (tabItem != null)
    {
    FrameworkElement view = tabItem.Content as FrameworkElement;
    TabControl tabControl = TryFindParent<TabControl>(tabItem);
    var regionName = RegionManager.GetRegionName(tabControl);
    var rm = container.Resolve<IRegionManager>();
    rm.Regions[regionName].Remove(view);
    }
    }
  2. If the RegionManager is another scoped RegionManager you could keep a reference to it after you add the view with the new scope, and either pass it as a parameter so it can be used in this method, or register it in the container (with an ID) so you have access to it. 

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman