UserControl/View's Access to it's Region

Topics: Prism v2 - Silverlight 2
Mar 20, 2009 at 12:11 AM
I am creating a default placeholder view, that my main application will populate all regions with on app start. As modules load each region will be populated again by the correct module. The idea being, I am working on ModuleX and it successfully replaced the Content region but didn't put any content in the Title region. This would be very obvious by seeing the "Title Region Place Holder" message in that region.

Trick is I want a single place holder view and for it to tell me what region it was loaded into. I tried the obvious:

var parent = VisualTreeHelper.GetParent(this) as FrameworkElement;
this.PlaceHolderText.Text = parent.Name ?? "Placeholder";

But this gives me a reference to the stack panel outer container. It seems that the regions are outright replaced by view is that correct? Anyone have a suggestion on how I can accomplish this?
Mar 23, 2009 at 3:17 PM
Wanted to come back and put an answer to this question as I was able to get a pointer from Glenn Block via Twitter. He suggested I iterate over the Regions collection of the regionManager. At first I wasn't thinking of this approach because I didn't want my views having any knowledge of prism as Glenn pointed out this was a violation of encapsulation. But this was a special case and intended to be a developer tool for viewing information about the region a "placeholder" view was displayed in.

My code ended up looking something like this:

public partial class PlaceholderView : UserControl, IPlaceholderView
  {
    private IRegionManager regionManager;

    public PlaceholderView(IRegionManager regionManager)
    {
      InitializeComponent();
      this.regionManager = regionManager;
    }


    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      foreach (IRegion region in regionManager.Regions)
      {
        foreach (UserControl view in region.Views)
        {
          if (view == this)
          {
            PlaceHolderText.Text =
              string.Format("{0} PlaceHolder : {1}x{2}",
                            region.Name, view.RenderSize.Width, view.RenderSize.Height);
            return;
          }
        }
      }
    }
  }