Shared UserControl Between Multiple Views in an MVVM RI Scenario

Topics: Prism v4 - WPF 4
Apr 12, 2011 at 3:20 PM

I have more than one view that needs the same controls. I extracted the common controls into a UserControl. So far so good. Where my problems is lies with separation of concerns. My subset of controls could easily make use of a ViewModel entirely unto itself. The questions is when does a UserControl become a View? If the UserControl should be a View, how do I share state from the View with its parent View? Is the simple MVVM RI (which works well for straight forward utility apps) not enough to support a composite view scenario? Thanks!

Developer
Apr 12, 2011 at 5:00 PM

Hi,

There are no special requisites for considering a UserControl a view. Therefore, you could treat your UserControl as a view, and vinculate it with a ViewModel. As for how to share the state between a view and a parent one, you could use Region Context.

From the Prism MSDN documentation, "Use this (Region Context) to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it."

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Apr 12, 2011 at 5:34 PM
However I'm not using regions.

Sent from my mobile device.

Please excuse its brevity and/or typographical errors.

On Apr 12, 2011, at 1:00 PM, "GuidoMaliandi"<notifications@codeplex.com> wrote:

From: GuidoMaliandi

Hi,

There are no special requisites for considering a UserControl a view. Therefore, you could treat your UserControl as a view, and vinculate it with a ViewModel. As for how to share the state between a view and a parent one, you could use Region Context.

From the Prism MSDN documentation, "Use this (Region Context) to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it."

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Developer
Apr 12, 2011 at 5:56 PM

Although the MVVM RI scenario does not need to use Regions, the Prism way to achieve the requirement you're mentioning should imply using Regions.

Another possibility would be to use other of the mechanism for communication between loosely coupled components to share a message from one View's ViewModel to the other one.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi