CoreCLR's Blog

Archive for January, 2011

Display a messagebox from the ViewModel using Prism’s InteractionRequests

Posted by coreclr on January 5, 2011

Prism has a nice feature called InteractionRequests. We can use this when the view-model needs to initiate an interaction, like displaying a messagebox.

Below is a simple example:

XAML:

In XAML I define the PopupChildWindowAction to display the dialog to the user.  The visuals of this dialog is defined in the SaveWindowsTemplate.
The magic here is the InteractionRequestTrigger from Prism. I set the SourceObject to the SaveRequest that is defined in the ViewModel.

SaveRequest is a InteractionRequest that define a Raise method, that we can use to raise the event.

I then use a interaction trigger to call the Cancel method in the viewmodel.  See Preview post.

ViewModel:

And the result look like this:

Advertisements

Posted in .NET, Silverlight | Leave a Comment »

Using interaction triggers in MVVM

Posted by coreclr on January 5, 2011

Commands are ofter to limited when using the MVVM pattern. Instead we can use interaction triggers.

Here is a simple example of handling the MouseEnter event for a button in a viewModel.

XAML:

ViewModel:

The key point here is the CallMethodAction object. CallMethodAction is a TriggerAction defined in the Microsoft.Expression.Interactivity.Core namespace. In this example I set TargetObject to the DataContext (the viewModel) and MethodName to the method we want to call in the ViewModel.

If you need to send parameters to the method, you just expose those as properties in the viewModel and use standard DataBinding.

Remember to reference Microsoft.Expression.Interactions.dll

Posted in .NET, Silverlight | Leave a Comment »