Reputation: 291
I am doing some data presentation using CM and WPF, and some of the data tabs have very similar formats but have to be kept in separate VM containing tabs as part of the standard for the application.
My initial thoughts were that I could do this programmatically in the VM by looking for any property pertaining to Views on the VM object (which itself is a Screen object derivation.) Its direct superclass is used as a contract for [ImportMany]
so that the parent VM and View can tabulate the collection.
[ImportingConstructor]
public PartiesMasterPartiesViewModel(
IEventAggregator events,
IHelpService help,
ResourceManager<B_Action> actionResource,
IActionService actionService)
: base( events, help, actionResource, actionService)
{
}
protected override void OnActivate()
{
base.OnActivate();
this.Views.Add(new KeyValuePair<object, object>(this,
new PartiesMasterListView()));
}
So either I am not using this property correctly, or it does not do what I thought it does and I need to use another way.
Another way I'm thinking of doing it this explicitly instantiating multiple instances of the same viewmodel and manually adding them to the collection, but this seems like it would be violating what MEF's [ImportMany]
would be here to do and weaken the design of the application.
Upvotes: 2
Views: 1045
Reputation: 10609
The simplest way to achieve a view shared by multiple view models is to configure the ViewLocator
with some extra rules.
In this example I have two view models Examples.ViewModels.SharedData1ViewModel
and Examples.ViewModels.SharedData1ViewModel
and a single view Examples.Views.SharedDataView
that I'd like to be the view that Caliburn.Micro locates for both by default.
In my set up code I can add the following simple regular expression to the ViewLocator
.
ViewLocator.NameTransformer.AddRule(
@"^Examples.ViewModels\.SharedData(\d+)ViewModel",
@"Examples.Views.SharedDataView");
Upvotes: 2