title: How to apply attached behavior right click to text block?
body: <p>I am working on a WPF application and doing my best to follow the MVVM architecture. I am using the GalaSoft MVVM light relay command implementation for all my commands and behavior.</p>
  
  <p>Currently, I am trying to learn and understand attached behaviors, and specifically, how to implement an attached behavior for the text blocks in my application.</p>
  
  <p>What I would like to do is have a style that I can apply to select text blocks that would execute some universal command (more on what I mean by "universal" later)</p>
  
  <p>Here is an example of what I want to do.</p>
  
  <p>For the example, I have two windows. Obviously in the real application I will  have many more, but this should suit my instructive needs.</p>
  
  <p>I'd like to apply an attached behavior to the text blocks in these windows that will implement a defined behavior.... Code...</p>
  
  <p><strong>Main Window</strong></p>
  
  <pre><code>&lt;Window x:Class="AttachedExample.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow"
          Height="350"
          Width="525"
          DataContext="{Binding Source={StaticResource Locator}, Path=Main}"&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock  Text="{Binding Path=SomeMainWindowModel.SomeText}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Main Window View Model</strong></p>
  
  <pre><code>public class MainWindowViewModel : BaseViewModel
  {
      private MainWindowModel _someMainWindowModel = new MainWindowModel();
  
      public MainWindowModel SomeMainWindowModel
      {
          get
          {
              return this._someMainWindowModel;
          }
          set
          {
              this._someMainWindowModel = value;
              this.NotifyPropertyChange("SomeMainWindowModel");
          }
      }
  }
  </code></pre>
  
  <p><strong>Main Window Model</strong></p>
  
  <pre><code>public class MainWindowModel : BaseModel
  {
      private string _someText = "Some main text for Stack Overflow!";
  
      public string SomeText
      {
          get
          {
              return this._someText;
          }
          set
          {
              this._someText = value;
              this.NotifyPropertyChange("SomeText");
          }
      }
  }
  </code></pre>
  
  <p>And now the secondary window....</p>
  
  <p><strong>Secondary Window</strong></p>
  
  <pre><code>&lt;Window x:Class="AttachedExample.SecondaryWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="SecondaryWindow"
          Height="300"
          Width="300"
          DataContext="{Binding Source={StaticResource Locator}, Path=Secondary}"&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock  Text="{Binding Path=SomeSecondaryWindowModel.SomeSecondaryText}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Secondary Window View Model</strong></p>
  
  <pre><code>public class SecondaryWindowViewModel : BaseViewModel
  {
      private SecondaryWindowModel _someSecondaryWindowModel = new SecondaryWindowModel();
  
      public SecondaryWindowModel SomeSecondaryWindowModel
      {
          get
          {
              return this._someSecondaryWindowModel;
          }
          set
          {
              this._someSecondaryWindowModel = value;
              this.NotifyPropertyChange("SomeSecondaryWindowModel");
          }
      }
  }
  </code></pre>
  
  <p><strong>Secondary Window Model</strong></p>
  
  <pre><code>public class SecondaryWindowModel : BaseModel
  {
      private string _someSecondaryText = "Some secondary text for Stack Overflow!";
  
      public string SomeSecondaryText
      {
          get
          {
              return this._someSecondaryText;
          }
          set
          {
              this._someSecondaryText = value;
              this.NotifyPropertyChange("SomeSecondaryText");
          }
      }
  }
  </code></pre>
  
  <p>What I want to do is be able to have one style in a resource dictionary or in the App.xaml that I can apply to each of these text blocks. The style would specify an attached behavior that would execute a method with the argument of the content of the text block, right clicked.</p>
  
  <p><strong>Pseudo Code</strong></p>
  
  <pre><code>*Right Click text block on MainWindow;*
  
  SomeStaticClass.ExecuteSomeStaticCustomMethod(mainWindowTextBlock.Content.ToString());
  
  *Right Click text block on SecondaryWindow;*
  
  SomeStaticClass.ExecuteSomeStaticCustomMethod(secondaryWindowTextBlock.Content.ToString());
  </code></pre>
  
  <p>That's a whole lot of example code and explanation to describe something that could be accomplished with an event handler in the code behind... but that wouldn't be following the MVVM pattern.</p>
  
  <p>Please remember I am using MVVM light in your replies.</p>