0
votes

On my MVVM silverlight application I have a dataDrid (in the View) and the dataGrid ItemsSource is binding to a Domain Service Classes Sql stored procedure 'results' in the XAML file. The stored proc 'Results' is defined in ViewModel which calls the WCF's stored proc on a 'Model' class.

I need to erase all items on dataGrid when user clicks on 'Clear' button but the ViewModel did not have any object reference to the View (the UserControl) to re-set its binding.

I am able to set the dataGrid's ItemsSource to NULL to erase all items on dataGrid but I did not know how to make the 'binding' again later in 'ViewModel' when the 'Query' button is clicked. Is there any api to get the reference of 'UserControl' in 'View' from 'ViewModel'?

Thanks for any help.

1
what I know is: your viewmodel of that particular view should be responsible of data publish on your view - where you have your DataGrid - as I understood your view is now connected directly to a service that is connected to a Stored procedure , bypassing your viewmodel is NOT MVVM , you should reference the service into your viewmodel and then bind your view to a collection in the viewmodel, hope this helps - Musaab
Thanks for your reply. I edited my question that the binded 'Result's is defined in ViewModel class so I need a reference of the View to re-binding the dataGrid's ItemsSource. If the View directly connects to WCF service than there is no problem to re-binding ItemsSouce from code behind. It seems most of code is moved to ViewModel from code behind and I need to know how to access some controls on View from the 'ViewModel' class. Thanks again. - CHI TANG

1 Answers

4
votes

If the grid has to be cleared, so should the ViewModel. The MVVM solution is to clear the collection in the ViewModel. The Grid will follow.