Pass parameter into ViewModel

Jun 5, 2012 at 5:04 AM

I'm stefanus ( Indonesian )
I have used mvvm light , csla.net and wpf.

I'm sorry for my english before.

I want to ask anyone about passing a parameter into view model that bound to a view.

the scenario is : I have a view model ( CustViewModel ) that bound to CustEditView.Xaml.
because i use csla.net i have a rich business object. so the question is :
1. On the New Scenario, I can used my CustViewModel with default constructor ( no param ).
    I follow your suggestion ( I read your bool SL 4 unleashed, and hear your video too ). I'm in the middle of learning a lot stuf for building my own application.
    there is no problem in this scenario, because I can do this :

    CustEditView dlg = new CustEditView();
    dlg.showDialog();

    I wired a CustEditView with CustViewModel from ViewModelLocator, from blend.
    So there is no problem on this scenario. ( CustViewModel is created when View is created ).

2. On the EDIT Scenario, I NEED to pass a parameter ( cust id ) to a view model ( CustViewModel ) [ BUT the reality is i have already bound / wire that ViewModel with View from blend ].
  
   I have already solve this problem in this way ,but I don't know is it good ? or any suggestion better ?
  
   Solution One :
  
   [ ON THE CustViewModel ]
   on the CustViewModel constructor i registered a message which is receive a cust id parameter. and then in the method that handle that message
   I load a cust object based on that parameter.
   If cust id is -1 ( my convention ) I load a new Cust Business Object.
   if cust id is no -1 ( which is cust id ), I load a Cust Business Object which have that cust id number.

   [ On The Caller CustEditView ] = [ Caller VIEW Code Behind ]
   I do these :
  
    CustEditView dlg = new CustEditView();
    Messenger.Default.Send<string>(cust_id); // this message will be handled with above message handler to load correct customer business object.
    dlg.showDialog();
 
    In this solution i still have an access on ViewModel from ViewModelLocator ( because CustViewModel is bound from ViewModelLocator property )

    Is this solution is good, or there is another better solution for this scenario ?

3. What I will want to try for another solution is :
  
    I will create CustViewModel from Caller VIEW Code Behind. Like this :

   CustEditView dlg = new CustEditView();
   dlg.DataContext = new CustViewModel(cust_id);
   dlg.ShowDialog();

   So I wire a ViewModel and View from Code Behind of My Caller View.
   On My Mind, I have a reasoning like this : I just used CustViewModel from ViewModelLocator for BLENDABILITY purpose when i'm doing design view on blend ONLY.
 
   AND when runtime I used another view model object (  different from ViewModelLocator property ), Is this OK ? Good ?


   In your opinion which is better solution no 2 or 3 ? could you explain the reasons for me ?

 

If there are another suggestions is okay ...

thanks a lot

 

 

stefanus

Nov 3, 2012 at 1:18 AM
Edited Nov 3, 2012 at 1:19 AM

Hi Stefanus,

I put together a CSLA .NET ViewModel adapter for MVVM Light Toolkit. There is a Model property for the CSLA business object and all functionalities of Csla.Xaml.ViewModel are available.

This is a pre-release and I'd like someone with MVVM Light Toolkit experience to test it. If you are interested, please contact me on http://www.codeplex.com/site/users/view/tiago so I can send you the DLLs and the source code. BTW it's build for MVVM Light 4.0.23 and CSLA 4.3.13.

Regards,

Tiago Freitas Leal