2

I have a WPF application with a simple tabular region layout as follows:

-----------------------------
|   Region 1  |   Region 2  |
-----------------------------
|   Region 3  |   Region 4  |
-----------------------------

I want to offer the ability to register modules and their position based on module/app configuration e.g. Register "Module1" in Region 1 and set its rowspan to 2 so that it overlaps region3 or perhaps register Module1, Module2, Module3 and Module4 in there respective regions, or alternatively register Module1 so that it consumes the entire grid.

My current thinking is to define the four regions in the Shell, load the modules from a XAML module catalog and set the row/column/rowspan/columnspan from the app.config however it would be ideal if there was some way in which I could extend the module catalog so that each module entry could directly specify the grid settings directly (much like attached dependency properties), I could then read these during module initalisation and register the modules in each region as required.

Hope this makes sense if not let me know and I'll try to clarify - I may well be overcomplicating things!

Sidebp
  • 770
  • 1
  • 10
  • 26
  • As I understend you want to load diferent views in some configurable combination of row/col/rowspan/colspan, but why? Is your layout going to change so often? – Rafa Castaneda Mar 02 '11 at 14:17
  • The principle is that the customer will purchase modules from a catalog of components e.g. a news feed, an image feed etc., they may purchase one in which case will want it to fill the whole screen, they may purchase 2 and in which case will want the layout to be different. It's not one, but think of a dashboard approach. I'm thinking MEF may give me more dynamic options than PRISM in isolation. – Sidebp Mar 02 '11 at 17:43

3 Answers3

2

First of all, in the question you are confusing views with modules. A module can be initialized without adding any views at all to your UI, or it may add several. So, assuming that in your case a module will expose exactly one type of view, it would be better to describe the setup as "add View1 in Region1" etc.

To solve your problem, you can use the Grid-based approach in your example like this:

  1. Declare your Grid in XAML with as many cells/regions as you want, just like your example. (Since your app will have a greatly dynamic layout, this could be better done manually in code. But if you 're willing to accept some hardwired row/column limits, XAML will work fine too and it will be simpler)
  2. Define a Prism event which your add-on modules will use to notify a "master" module that they have been initialized. The master module will load before any add-on module and take care of the layout. When raising this event, the add-on modules will include the types of views they have configured the container with as part of the event arguments.
  3. When initializing, the master module will subscribe to the "module initialized" event. Whenever the event is raised (an add-on module has been initialized), the master module will resolve a view of the appropriate type from the container (remember: the add-on module has told us what type of view to resolve).

It is then very simple to achieve your goal like this:

var view = container.Resolve(typeOfViewFromAddOnModule);
var uiElement = (UIElement)view; // because that's what it's going to be
Grid.SetColumn(uiElement, X);
Grid.SetRow(uiElement, Y);
Grid.SetColumnSpan(uiElement, Z);
Grid.SetRowSpan(uiElement, W);
var region = // get a reference to your Grid region here
region.Add(view);

You are free to determine the values of X, Y, Z, W in whatever way you wish (load them from configuration, auto-assign them if they are not present, have the add-on module specify them in the "module initialized" event, etc etc).

Jon
  • 428,835
  • 81
  • 738
  • 806
1

For your use case I would consider, instead of setting four regions in a grid, set only one region in an ItemsControl with some ItemsPanelTemplate to suit your layout requirements, like a WrapPanel for instance:

<ItemsControl regions:RegionManager.RegionName="SomeRegion">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate> <toolkit:WrapPanel /> </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Rafa Castaneda
  • 812
  • 1
  • 7
  • 13
1

Use view injection approach in which you'll have a IGridLayoutService with method AddView(object content, int row, int col, int rowSpan, int colSpan);

so you'll have a single region that is being controlled by some class implementing IGridLayoutService which adds a single view in it that has a Grid. All the views passed in AddView are added as children of the grid with the specified properties.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131