Show / Hide Views?

Topics: Prism v4 - Silverlight 4
Dec 20, 2010 at 3:53 PM

I seem to be struggling with dynamically showing and hiding views.  I have an application that uses a Menu that has checkbox MenuItems.  When the user checks the box the view is supposed to show up, unchecked it goes away.  Pretty simple concept but the implementation is eluding me.

I can brute force remove a view using this code:

IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
IRegion region = regionManager.Regions[regionName];
region.Remove(region.ActiveViews.First(x => x.ToString() == viewName));

and have tried a long series of combination to get the view to come back with no luck.

Any help would be greatly appreciated.


Dec 20, 2010 at 4:22 PM

Hi Mark,

You might find the Navigation chapter from the Prism MSDN documentation useful to your purpose. From the documetation "Frequently, navigation means that certain controls in the UI are removed, while other controls are added."

In order to add the view back when you have removed it, you could for example make use of navigation or View Injection (which is explained in this chapter from the MSDN documentation) to inject the view back when you want it again in your region.

Additionally, you might find the following threads useful, as they deal with similar scenarios (without using Navigation, however, as it is a feature introduced in Prism v4 and the posts are from an older date):

I hope you find this helpful.

Guido Leandro Maliandi

Dec 20, 2010 at 4:38 PM


Thanks for the quick response.  I will definately read through those post and see what i can come up with.

But.. this hackery got me through.. :).. MainRegion being an ItemsControl, vis being System.Windows.Visibility variable i set based on the checkstate of the MenuItem... Lame I know.. sorry had to move onto other task

UserControl c = ((UserControl)MainRegion.Items[0]).ChildrenOfType<UserControl>().First(z => z.ToString() == viewName) as UserControl;
c.Visibility = vis;