RegisterViewWithRegion and viewname

Topics: Prism v2 - WPF 3.5
Sep 1, 2009 at 11:54 AM

I have this code to register a view in a region:

_regionManager.RegisterViewWithRegion("myMainRegion", () => _container.Resolve<ISomeViewModel>().View);

but elsewhere, I really need to be able to switch views by using the view name. How can I set the view name and use the above code?

I tried using

_regionManager.Regions["myMainRegion"].Add(new SomeView(), "myViewName");

but the essential Resolve-ing does not then take place - for example, the view model constructor is never called.

I am new to Prism and containers (Unity), so any help would be greatly apreciated

James S

Sep 1, 2009 at 12:45 PM
Edited Sep 1, 2009 at 12:46 PM

In your view's XAML use x:Name="myViewName"

Sep 1, 2009 at 2:34 PM

Thanks - annoying how one misses the simple and obvious!

BUT - there is still a problem... in the Prism source, in Region.cs, class Region, we have the GetView funtion for getting a named view in the region, with the essential line:

ItemMetadata metadata = this.ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName);

In debug, you can see that x is indeed the right view and a base class does have the name given in the XAML, but x.Name is still null here and so the function reports that the view is not in the region.

Not sure what to do now...

James S

Sep 1, 2009 at 3:13 PM

I changed the above line to:

ItemMetadata metadata = this.ItemMetadataCollection.FirstOrDefault(x => ((System.Windows.FrameworkElement)(x.Item)).Name == viewName);

and it now works (I added an additional GetView function rather than change the original code).

The views, in my case, are dynamically-added User Controls and I did add x:name="NiceView" (or whatever) to the XAML in the user control.

I feel this is but a hack - any other ideas?

James S



Sep 1, 2009 at 3:55 PM

Forest through the trees - been there, done that ;)

[jastewart] but elsewhere, I really need to be able to switch views by using the view name. How can I set the view name and use the above code?

I'm not sure this will help but I'll offer it just in case - I have a MenuRegion that holds buttons; each buttons Tag property holds the View to activate it.  Code follows:

/// <summary>
/// Handles the Button Click - activates the view specified in the button's Tag 
/// for the "MainRegion"
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
void buttonClickHandler(object sender, System.Windows.RoutedEventArgs e)
    // Cast sender to button so we can access it easier
    Button button = sender as Button;

    Logger.Log(string.Format("Button Click: {0} for View: {1}",
      button.Name, button.Tag), Category.Debug, Priority.None);

    aggregator.GetEvent<StatusEvent>().Publish(new StatusEventArgs
        Message = string.Format("View {0} selected", button.Tag),
        StatusPanel = StatusType.Left  // Left status bar

    // Disable the button just clicked
    button.IsEnabled = false;

    // Get all views that are using MainRegion
    IRegion mainRegion = regionManager.Regions["MainRegion"];

    // Activate the view if the view name matches the current
    // button's tag value.  
    foreach (IViewBase view in mainRegion.Views)
        if (view.Name.Contains(button.Tag.ToString()))



Sep 1, 2009 at 4:17 PM

Thanks for the code sample - I have a similar context (menu region and main content region in which user controls are switched in and out) but am implementing things the MVVM way (so no code-behind!).

To do the switching, the view model is bound to the view, but the essential code in the menu region is:


where each view subscribes to EventShowOneView (I will come up with a better name) which delivers the viewname of the view required to be shown. A view consuming this event will then show itself if the name is theirs, else it hides itself. e.g.


I also made use of the Show/Hide fcns in WPFRegionExtensions  - see  That show/hide code could be useful in factoring out your foreach loop... poss replace with one line:  regionManager.Regions["MainRegion"].Show(button.Tag.ToString());

With my kludgy hack (see prev post) I have a workable solution - at least until the next gotcha rears its head!

James S


Sep 2, 2009 at 12:17 PM

Thanks for the info - I'll have to read up on the blog (show/hide to refactor out loops).   The code snippet actually resides in the views presenter (MVP-MVVM patterns); like yourself I don't put code in code-behind.