MVVM Best Practices?

Topics: Prism v1, Prism v2 - WPF 3.5
Oct 30, 2009 at 7:29 AM

Hello,

In my application we are using MVVM with WPF, I wanted to find out what is the right to send the UI objects in the form of sender & eventargs to ViewModel?

As per my understanding ViewModel has to be independant of View, so in future if the new UI comes it shoud be easily integretable to ViewModel.

I tried to identify the ways to avoid passing the Sender & Eventargs from UI to ViewModel, but realised some rich UI features need those arguments to be passed to ViewModel.

Does it mean this application is not feasible for MVVM? I went through lot of articles on net but didn't get any clue about this question.

 

It would be really nice if anyone can put light on this.

 

Thanks in advance.

Oct 30, 2009 at 9:04 AM

Yes. You should never pass the Sender or EventArgs to ViewModel.

>>realised some rich UI features need those arguments to be passed to ViewModel.

Could you please let us know the details of your scenario? There are only two problems that we used to face. a) you can to pass the object or data more than one. b) your control doesn't have any bindable property. In order to solve both cases, you can use the custom attached property .

If your problem is different one then please let us know. We are very interested to know more scenerio.

Regards,
Michael Sync

Silverlight MVP & Microsoft WPF/Silverlight Insider.

Blog: http://michaelsync.net

 

Oct 30, 2009 at 10:13 AM
Edited Oct 30, 2009 at 10:34 AM

Hi Michael,

Thanks for your reply. Few scenerios which I can recollect now are :

1) On button click I want to show the messagebox & I want to assign the owner to that messagebox(that is parent window).

2) How can I set the DataContext on UserControl?

3) Sometime you need to focus on a particular action say when a user clicks on OK, we want to save the data & focus has to go to Contro1 on the form.

4) There is a datagrid in which 1st column is the drop down, user selects some type of transaction code(functional term), based on that 2nd column is populated with data for that transaction code.

5) How to put DataContext change event in ViewModel, requirement is when the datacontext is changed, I want to refresh particular section of the form.

Regards

Deepak

 

Oct 30, 2009 at 11:07 AM
Edited Oct 30, 2009 at 11:07 AM

>>1) On button click I want to show the messagebox & I want to assign the owner to that messagebox(that is parent window).

There are two ways for that.

1. You can take a look  at this article. http://www.codeproject.com/KB/architecture/MVVM_Dialogs.aspx?display=Print

2. You can have View interface in your ViewModel and use the constructor injection to inject the view.  (You shouldn't have strong view reference in VM but you can have an interface of your view if needed.)  then, you can set it like view.ShowMessage(yourmessage); Then, you can set the owner in your concrete view. 

Example:

interface IView{

  void ShowMessage(string message);

}

class View : IView {

    public void ShowMessage(string message){

              MessageBox.Show(this, message);

    }

}

class ViewModel{

private IView view;

public ViewModel(IVew view){

  this.view = view;

}

///Call this in On Command.

view.ShowMessage("This is a msg");

}

 

>>2) How can I set the DataContext on UserControl?

Normally, you can set the ViewModel to the DataContext of View at Controller or Module. You can read how to do this in UI Composition Sample.

>>3) Sometime you need to focus on a particular action say when a user clicks on OK, we want to save the data & focus has to go to Contro1 on the form.

You can use the way that I mentioned in my #1 answer.

>>4) There is a datagrid in which 1st column is the drop down, user selects some type of transaction code(functional term), based on that 2nd column is populated with data for that transaction code.

- Bind the selected item of Combobox of 1st column to the Property1 of ViewModel

- Bind the ItemSource of combobox of 2nd column to the Property2 of ViewModel

- In the setter of Property1, you can repopulate the data to Property2.

>>5) How to put DataContext change event in ViewModel, requirement is when the datacontext is changed, I want to refresh particular section of the form.

Why do you want to change the DataContext of ViewModel? I don't understand this question. Could you please tell me more for this one? I may be able to suggest other alternative way to achieve it.

Regards,
Michael Sync

Silverlight MVP & Microsoft WPF/Silverlight Insider.

Blog: http://michaelsync.net

Oct 30, 2009 at 11:30 AM

Firstly thank uuuuuuuuu so muchhhh for replies. I am still going through the articles which you have mentioned.

About DataContext change of ViewModel question: Sorry for the confusion. Currently we have a view which has a container,which contains few controls & we have a datacontext change event which refereshed that container. Currently this code is in code behind & we want to move it to ViewModel, can you propose approach for that.

 

Oct 30, 2009 at 8:30 PM

Hi

take a look to this thread.

http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=66164

I explained how to get EventArgs in your viewModel. May this can hel you get also the sender.

Let me know

Thanks

 

Blochaou Francois