views:

161

answers:

2

Hi,

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 to get this data input. Will this comply with MVVM...if not, what is the way for the same?

+1  A: 

I'm afraid this won't be correct in MVVM design pattern. try to stick to your view model to define properties. Why don't you consider moving that property to control's vm?

Piotr Justyna
If I have the property in View-model (not view)....How I can set its value from the page..where I use this usercontrol ?
Anish
Are you binding it properly? For example: <TextBlock Text="{Binding Path=YourProperty, Mode=TwoWay}" />
Piotr Justyna
Hi Justyna, I will explain u my issue more... I have created a user control in wpf following mvvm. In my view..i have a gridview. When I use my user control in a page...i have to assign a dataset (which is available in my page) to the gridview available in usercontrol... How do I set it ?... I thought of having a property defined in view's .cs file..and assign it a dataset from my page.But I wonder if it comply with mvvm :(
Anish
If your dataset is defined in Page's VM, user control's VM must be able to reach it - you have to figure it out yourself. IMO you should not think of binding your dataset to the control, but of binding data source for that dataset to your control. You can make your datasource available by passing it as a constructor parameter to your user control.
Piotr Justyna
And the other thing - you will greatly improve the number of views if you start giving points for answers.
Piotr Justyna
u mean clicking "right button" ?
Anish
What do you mean by "right button"?
Piotr Justyna
to give the point ...do i have to click "tick" label...?
Anish
to give a poitn just click the "up" triange on the left hand side of the answer. And the other thing - please report if you've solved your problem :)
Piotr Justyna
am not able to give u point ..as i don hav 15 reputation :(My issue is not solved yet...
Anish
Thanks for the point. What Veer says is simply to bind your user control's gridview to datasource which is also used by your page. This is actually the same answer as mine. Please think of sharing a datasource (it doesn't matter if this is a List, ObservableCollection, Array or DataReader) by both UserControl and Page.
Piotr Justyna
How the user control's viewmodel can be accessed from page's vm?Because...when we refer the namespace of usercontrol's view model in page's viewmodel...we do not know which class it has to refer :(
Anish
It's best not no let VMs know about each other, but to share their datasources. IMO the best idea would be to construct your user control passing your datasource from Page as a reference. please think about it and try it.
Piotr Justyna
+1  A: 

Use an ObservableCollection rather.

ObservableCollection<myModel> myOC = new ObservableCollection<myModel>();

where myModel is a class that has to be constructed transforming your columns in the DataTable to Properties.

In your MainViewModel, loop through the DataReader and create myOC out of it.

Now bind myOC to a ListView in your page.

The DataTemplate of ListView should be a view(UserControl) drawing data from a ViewModel constructed out of myModel

But your UserControl has the entire ListView inside. If that is on purpose, then let me know the entire design to give a better idea.

Veer
I am using user control in different pages - means i will be having different datatable for different pages...so creating OC out of it is difficult as we need to have a class "myModel" with us.The main issue which i am facing is what to do with accessing the page's data in user control.
Anish
@Anish: What does each of your UserControl consist of?
Veer
I didnt get you :(Anyway, I have only one user control...which has 2 gridviews and 4 buttons(add/add all/remove/remove all)...this user control is for adding/removing some data from one gridview to another
Anish
In that case, I think it is better to create two properties for the UserControl as you said. It will comply with MVVM.
Veer