views:

384

answers:

1

Hi there,

I have a business object, lets say customer, it has too many fields to show in one page, so I'm splitting it to different tab pages. the data is not in a way that I can split it into meaningful bits (like address, having state, street name and so on), so I decided to keep the same model and view model and have different views (each tab page content is a different view) bind to the same view model.

first of all is this the correct approach and why not?

secondly, if it is, how do I use unity to pass the same view model to child views? at the moment I use constructor injection, but they are new instances of view model.

Cheers,

Ali

+2  A: 

If you're using Prism, then you can use RegionContext.

Specify RegionContext for TabControl:

<TabControl cal:RegionManager.RegionName="MyTabControl"
cal:RegionManager.RegionContext="{Binding Path=ViewModel}">

And access it from Views added to this region using static GetObservableContext method on RegionContext:

void ViewConstructor()
{
this.ViewModel = (MyViewModel)RegionContext.GetObservableContext(this).Value;
}

If you are not using Prism, then you can register your ViewModel as a named instance:

Container.RegisterInstance<IMyViewModel>("viewModelName", new MyViewModel());

and get it later using:

Container.Resolve<IMyViewModel>("viewModelName");
jarek