0
votes

In my silverlight application I need to send a notification from a ViewModel to a View. In response to it a method on a UI control should be called. I know about 2 ways to accomplish this:

1) Raise an event in the ViewModel and handle it in the View's code behind.
2) Send a message from the ViewModel (using the MVVM Light messaging support) and respond to this message in the View's code behind.

I'd like to know if there is a way to accomplish this without using code in the View's code behind, for instance through some kind of data binding in the XAML?

Please share any ideas.

Additional info about what the View should do when it receives notification from ViewModel
In the XAML of the View I declare an instance of a custom Silverlight grid control which has the following method:
public void FileExportFinished(bool fileExportSucceeded)
I want to call this method from the XAML in response to the notification received from the ViewModel passing a boolean value received with the notification.

2
It depends on what thing you want to handle, where you want to handle it and how you want to handle it. Maybe DataTrigger?dvvrd
Can you give a little more explanation of what you are trying to handle?Shayne Boyer
I have updated the question, please check it.Narek Malkhasyan
@dvvrd I don't think Silverlight has DataTriggercadrell0
@Narek It is OK to have Code Behind on your View. It does not violate MVVM to do this, as long as this code is View logic. The real goal of MVVM is no business logic in your Code Behind.cadrell0

2 Answers

3
votes

Yes...you can do it with the help of a dependency property.

  1. Create a dependency property for that view (Make it boolean type since we just need this property to call another view method).
  2. In its property changed call back, make provisions to call your required view method.
  3. Then bind the DependencyProperty with a propert in ViewModel.
  4. So when you need the view to be updated, just set the binded Property mentioned above, this will fire the property changed call back of Dependency property and form there your required view method will be called.
0
votes

why not simply use a Property in your viewmodel and a DataTrigger in your xaml?

if you want some kind of dialog popup you can use a dialogservice. you should really add what you wanna do to your question. what should happen in your view when the notification arrive?

btw Messenger is for viewmodel-viewmodel communication, so thats not an option.