Getting confuse with views and Locator

Dec 2, 2011 at 1:30 PM

Dear all,

I have started to implement a MVVM style in  my new application but I am really confuse on what views should exaclty do and what locator hsoud really do.
What I have seen so far is really sample basics with one single view in my case I have an application start which is suppose to collect a list of produc from a database through a WCF service.

So for that I have following Views :
MainWindowView which is actually conatining nothing els as a list box
A ProductView which is containing a UserControl to represent a product

Then I have associated ViewModel as :


I have an extrnal class call ProductsProvider which is in charge to retrive product information through the WCF service which is define as follow :

 class ProductsProvider
        private IList<Product> _products;

        public IList<Product> Products
            get { return _products; }
            set { _products = value; }
        private IProductService _serviceClient;
        private static ProductsProvider _Instance;

        private ProductsProvider(IProductService serviceClient)
            this._serviceClient = serviceClient;
            this._products = serviceClient.GetProducts();

        /// <summary> 
        /// Create single instance of <see cref="ProductsProvider"/> 
        /// </summary> 
        public static ProductsProvider InstanceObject(IProductService serviceClient)
            if (_Instance != null)
                return _Instance;
            else return new ProductsProvider(serviceClient);


What I do not know now is how to setup my different ViewModel's in order to get correctly product information bind to the items source of the list box in my mainvindow ?
Thnaks for your help
If needed I can provide my sample project for better view as a link
Dec 2, 2011 at 2:21 PM


This sounds very similar to the JsonDemo sample I made at MIX11. Check for the video and for the code.


Dec 3, 2011 at 2:40 PM

I check it but I have a wierd situtation mayb e you can explain.

Let say that I have 3 ViewModels named ViemModel1...3 with their corresponding views.
So if I catch correctly the whole thing, I should add in the ViewModelLocato the creating of viewLModel 1..3 with a MainStatic property which will then call a Create method.

Form that points comes my first question :

In Default MVVM project template, the MainView is bind as follow :

 DataContext="{Binding Main, Source={StaticResource Locator}}">

So through the Main property , the Locator is creating the MainViewModel, sor here is my first question :
1 - In real scenarios, does the Main property on the Locator should create all other ViewModels (1..3) or does ViewModels1...3 will have also their own Main property where their respective views wil be bind to ? ?

So lets add to Views corresponding Views1...3.

Here comes my second question :

2 - Deos each views will have there datacontext bind to a kind of Main property in Locator, or will it be bind to a Class in the their ViewModels ?

Thanks for clarification on which path shold I go for reall application. When we look video it is always simple but when we try to implement it a real project, things start to be confuse and not anymore like simple exemple.
For exemple, having a view bind to the Locator which create an instance of the view and have in its constructor the need to call a service through an external gateway class make it fails.
In such cas in order to work I add to bind my view dataContext as follow :
DataContext="{Binding Source={x:Type vm:ProductViewModel}}">
And here I am not binding through Localtor class as you cann see, so this is the reason why I do not know what it right or wrong
Thnaks for help