views:

81

answers:

3

I have a WPF user control ...which is in MVVM. The user control(which contains a listview) need data from the page (where it is included). I have to set a property in View's code behind to get this data input. Will this comply with MVVM(But MVVM pattern do not support adding code in code behind file of view as far as i know).if not, what is the way for the same?

+1  A: 

You want to do this via data binding. The controls are bound to properties in your viewmodel which receives the data, applies the needed logic and gives it back to the view for displaying it. Have a look here to get an idea on how all that works.

AKrebs
I have gone through this...But..still i have athe same doubt :(I need properties for user control...and where do i declare it ?
Anish
A: 

I have got a link : http://social.msdn.microsoft.com/Forums/en/wpf/thread/a3eedc3e-0d59-420c-aba0-44fe8b00552f

But I'm not really getting whats meant by injection in it (as given below) :

an interface to the UserControl public model called IUserControlModel. It has the properties that should be visible from outside; - a UserControlViewModel that contains a public property of type IUserControlModel that is injected in the constructor; plus all the properties used for XAML binds specific to the usercontrol implementation; XAML may have binds directly to the IUserControlModel properties too; - a MainWindowViewModel that nests the IUserControlModel inside.

Anish
A: 

I think your problem can be solved in easier way. If you expose ItemsSource property of ListView as Dependency Property of your user control you can achieve what you want without unnecesary (in this case) overhead of implementing MVVM pattern : You then can just use databinding to add data from the page where the user control is included.

post that I think answers your question : Post link

Michael
The Client need the user control in mvvm pattern :( The issue here is I NEED TO DEFINE A USER CONTROL PROPERTY (not in code behind) WHICH I CAN SET FROM BASE PAGE...
Anish