Using Prism’s InteractionRequests, view models can make interaction requests (such as notifications or confirmations) to the view. It seems that WPF support and documentation lacks behind Silverlight, though. Therefore, I compiled a quick example using InteractionRequests in WPF to trigger a message.
View Model
using Microsoft.Practices.Prism.ViewModel; using Microsoft.Practices.Prism.Interactivity.InteractionRequest; public class MyViewModel : NotificationObject { // view can bind to this request public InteractionRequest<Notification> MessageRequest { get; private set; } // constructor public MyViewModel() { MessageRequest = new InteractionRequest<Notification>(); } // trigger message private void RaiseMessageRequest(string message) { MessageRequest .Raise(new Notification() { Title = message }); } }
Action
The action defines what is to happen in the view. I defined a dependency property to a TextBox to display the message (bound to in the view).
using Microsoft.Practices.Prism.Interactivity.InteractionRequest; using System.Windows.Interactivity; public class ShowMessageAction : TriggerAction<FrameworkElement> { // element in view where message will be shown public static readonly DependencyProperty TitleBoxProperty = DependencyProperty.Register("TitleBox", typeof(TextBox), typeof(ShowMessageAction)); public TextBox TitleBox { get { return (TextBox)GetValue(TitleBoxProperty); } set { SetValue(TitleBoxProperty, value); } } protected override void Invoke(object parameter) { InteractionRequestedEventArgs e = parameter as InteractionRequestedEventArgs; if (e != null) { if (TitleBox != null) { TitleBox.Opacity = 1.0; TitleBox.Text = e.Context.Title; // TODO: for example, hide after some time } } } }
View
Required Namespaces
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:prism="clr-namespace:Microsoft.Practices.Prism.Interactivity.InteractionRequest;assembly=Microsoft.Practices.Prism.Interactivity"
Displaying Message
<TextBox Opacity="0.0" Name="MessageTitleBox"/> <i:Interaction.Triggers> <prism:InteractionRequestTrigger SourceObject="{Binding MessageRequest}"> <v:ShowMessageAction TitleBox="{Binding ElementName=MessageTitleBox}"/> </prism:InteractionRequestTrigger> </i:Interaction.Triggers>
5 comments
David Bitton says:
April 25, 2011 at 19:59 (UTC 1 )
Found a prob, and fixed it. You have a TextBox in the XAML sample, however the DependencyProperty is typed as TextBlock. This results in a null TitleBox property in the ShowMessageAction class. Once I changed from TextBlock to TextBox, all worked.
Dominik says:
May 3, 2011 at 15:26 (UTC 1 )
Hi David, thanks for catching this. I corrected the example. Dominik
Samir says:
July 18, 2011 at 20:06 (UTC 1 )
I think you are right. PRISM WPF Documentation lacks this InteratcionRequest feature.Where is the Source Code?
Regards,
Sampat
Pierre says:
April 23, 2012 at 15:56 (UTC 1 )
I just started with WPF and MVVM and am interested in a working sample application using the code above. Is it available somewhere?
Dominik says:
April 24, 2012 at 08:50 (UTC 1 )
Hi Pierre, I can’t find the source project anymore, but I hope the crucial bits and pieces are shown above.