Ribbon Region Removing issue

Topics: Prism v4 - WPF 4
Jan 4, 2012 at 11:07 AM

Hi All,

I am using the following code for remove the Ribbon Tab

          foreach (var view in rmanager.Regions[RegionNames.RibbonRegion].Views)
            {
                if (view != null)
                {
                    RibbonTab tab = view as RibbonTab;
                    if (tab.Name == "LoginTabView")
                    {
                        rmanager.Regions[RegionNames.RibbonRegion].Remove(view);
                    }
                }
                continue;
            }

 

It throws the error as follows :

" {"The region does not contain the specified view.\r\nParameter name: view"} "

I debugged the project in Ribbon RegionAdapter class Ribbon Tab is removed but it throwing above exception.

Thanks and Regards,

Sakthi

Developer
Jan 4, 2012 at 5:18 PM

Hi Sakthi,

Based on my understanding, you might find the following thread useful, where a scenario in which the same exception is being thrown when the views are removed:

Regarding the region adapter, as far as I know there is no official support for ribbon region adapters in Prism out of the box. However, you can check the following blog post where an example of a ribbon region adapter is provided. The blog post is in German but it provides a sample solution that could be useful regardless of your understanding of the language:

Also, if you believe that ribbon region adapters should be supported by Prism out of the box, you can vote in the following work item so that the prism team analyzes it for a future release:

As a side note, it can be seen in the code snippet you posted above that the collection that is being iterated in the foreach statement (the Views collection of the region) is modified inside the foreach when the Remove method of the region is invoked. Based on my understanding, this might cause unexpected behaviors in your application. Just to be safe, it might be useful to modify the collection outside of the foreach statement. For example:

IRegion region = rmanager.Regions[RegionNames.RibbonRegion];
List<Object> viewsToRemove = new List<Object>();

foreach (var view in region.Views)
{
    if (view != null)
    {
        RibbonTab tab = view as RibbonTab;
        if (tab.Name == "LoginTabView")
        { 
            viewsToRemove.Add(view);
        }
    }
}

foreach(var view in viewsToRemove)
{
    region.Remove(view);
}

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini