Click here to Skip to main content
15,887,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a small app for learning purpose . in my shell I took a itemsControl which defines a region and I have registered three views to this region,in my shell I have one another region and registed a view to it,the view contains 3 buttons,my requirement is that when I click on button1,view1 get highlighted and other 2 view either disapper or disable,same way when click second button,view2 should be liglllited.
I am trying to achive this since 4 days but no luck.
any help would be appericiated.
Posted
Comments
Martijn Kok 14-Aug-12 8:13am    
In general you need to go through 3 steps:
1. Get a reference to the RegionManager
2. Get the Region with the 3 views
3. Get the view and enable/show the wanted view and disble/hide the others


On which step do you need help? If you need help on getting the RegionManager I have an additional question. Are you using Unity and a UnityBootstrapper?

1 solution

Your first concern is to get a reference to the RegionManager. So far I know of 2 ways to get the regionmanager of your shell. Both methods will also word when you are in another view or a viewmodel. There might be other methods using MEF or other Dependency Injection systems (like Castle Windsor).

The first method assumes that you use Unity and have used an implemenation of the UnityBootstrapper. This method is descibed in the Prism4.chm file which you probably have found in Prism directory (where you did install prism). It is in Chapter 2 (Initializing Prism applications).

Once you have run the bootstrapper you can use the ServiceLocator to locate the default RegionManager (the shell uses the default RegionManager).

C#
// Import the Microsoft.Practices.Prism.Regions, Microsoft.Practices.ServiceLocation and (maybe) Microsoft.Practices.Unity

IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();


The second method makes it possible to get a reference to the RegionManager without using a Dependency System. This uses a cusom implementation of a RegionBehaviour. This is descibed in http://stackoverflow.com/questions/6582612/prism-4-locally-scoped-regionmanager[^]. Using the code in this answer you can make View and ViewModel aware of it's RegionManager.


Once you have the regionmanager you will need a reference to the region containing the 3 views.
C#
IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];


To get a reference to the view, you use
C#
//use namespace System.Linq

FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;



Putting it all together and assuming that you use code behind to handle the clickevent of the buttons, you might get something like this:

C#
private void Button1_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(0);
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(1);
}

private void Button3_Click(object sender, RoutedEventArgs e)
{
        ShowViewAtIndex(2);
}

private void ShowViewAtIndex(int index)
{
    IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];

    int numberOfViews = myRegion.Views.Count();
    for (int i = 0; i < numberOfViews; i++)
    {
        FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;
        if (aView != null && i == index)
            aView.Visibility = System.Windows.Visibility.Visible;
        else
            aView.Visibility = System.Windows.Visibility.Collapsed;
    }
}


I hope this is of any use.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900