«

»

Mar
06

.NET/C# Prism InteractionRequests in WPF

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

  1. David Bitton says:

    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.

  2. Dominik says:

    Hi David, thanks for catching this. I corrected the example. Dominik

  3. Samir says:

    I think you are right. PRISM WPF Documentation lacks this InteratcionRequest feature.Where is the Source Code?

    Regards,
    Sampat

  4. Pierre says:

    I just started with WPF and MVVM and am interested in a working sample application using the code above. Is it available somewhere?

  5. Dominik says:

    Hi Pierre, I can’t find the source project anymore, but I hope the crucial bits and pieces are shown above.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">