Removing view

Aug 26, 2008 at 10:32 AM
Is it possible to remove a view from a region without having to hardcode the view name. I'm using the PresentationModel pattern.  Most of my views are loaded from the module init or from a supervising controller.  I'm ok in hard coding the region when I launch the view but once it's launch I would prefer no have to hard code the region name to be able to remove the view from region. Is there a better way? 
Aug 28, 2008 at 5:27 AM
Hi martalex,

It is possible to remove a view from a region without knowing its name.
The following example demonstrates this:

foreach(object view in new List<object>(RegionManager.Regions["MyRegion"].Views)) {
  RegionManager.Regions["MyRegion"].Remove(view);
}

The list construction is required to prevent the application from throwing EnumerationModifiedException instances.
Hope this helps.
Sep 2, 2008 at 12:55 AM

martalex,

Another approach you could take for ContentControl placeholders is remove the Content value from ContentControl.

<!--View.xaml-->
...
<ContentControl x:Name="childArea" cal:RegionManager.RegionName="ViewPlace"/>
<Button x:Name="removeView" Content="Remove View" Click="buttonRemoveView_Click"/>
...

// View
void buttonRemoveView_Click(object sender, RoutedEventArgs e)
{
 // TODO: check for null region name
 controller.RemoveView((string)childArea.GetValue(RegionManager.RegionName), childArea.Content);
}

// Controller
internal void RemoveView(string regionName, object view)
{
 // TODO: check for null region
 RegionManager.Regions[regionName].Remove(view);
}

You should be able to inject the IRegionManager into your controller.

Hopes this helps,

Andres Olivares