title: System.Windows.Interactivity.EventTrigger in codebehind works on some Controls, but not others
body: <p>I have an application where I'm adding controls during runtime and want to attach <a href="http://msdn.microsoft.com/en-us/library/system.windows.interactivity.eventtrigger%28v=expression.40%29.aspx" rel="nofollow noreferrer">EventTrigger(s)</a> that are <a href="http://msdn.microsoft.com/en-us/library/microsoft.expression.interactivity.media.playsoundaction%28v=expression.40%29.aspx" rel="nofollow noreferrer">PlaySoundAction</a>.  In XAML this is easy, but with the need to generate controls on the fly and add these Triggers it becomes a bit more difficult.</p>
  
  <p>I asked in a <a href="https://stackoverflow.com/questions/8088770/how-to-add-eventtrigger-with-soundactions-in-code-behind">previous question</a> and was able to get part way there and with recent testing I have come to a conclusion on what I can do to solve it, but was hoping there might be something I'm missing.  I am able to get the EventTrigger working on a Button control, but not on a <a href="http://msdn.microsoft.com/en-us/library/microsoft.expression.shapes.regularpolygon%28v=expression.40%29.aspx" rel="nofollow noreferrer">RegularPolygon</a> and I'm unable to take an easy route on embedding a button inside of the RegularPolygon to achieve the results I want because it is not a ContentControl.</p>
  
  <p>This is what I currently have this method for adding new RegularPolygons to a container (Canvas):</p>
  
  <pre><code>public RegularPolygon AddShape(Panel container)
  {
      RegularPolygon shape = new RegularPolygon();
  
      // ShapeColors is a List&lt;SolidColorBrush&gt; that has been prepopulated
      // _random is a Random() from .NET
      shape.Fill = ShapeColors[_random.Next(0, ColorCount - 1)];
      shape.Stretch = Stretch.Fill;
      shape.PointCount = _random.Next(3, 6);
      shape.UseLayoutRounding = false;
      shape.Stroke = new SolidColorBrush(Colors.Black);
      shape.Width = _random.Next(50, 150);
      shape.Height = _random.Next(50, 150);
      // UI_Tap is an arbitrary function for handling the Tap event
      shape.Tap += UI_Tap;
  
      System.Windows.Interactivity.EventTrigger trigger = new System.Windows.Interactivity.EventTrigger("Tap");
      PlaySoundAction correct = new PlaySoundAction();
      correct.Source = new Uri("/Sounds/Correct.mp3", UriKind.RelativeOrAbsolute);
      correct.Volume = 1;
  
      trigger.Actions.Add(correct);
  
      //Button b = new Button();
      //b.Content = "This is button B";
      //System.Windows.Interactivity.Interaction.GetTriggers(b).Add(trigger);
      //container.Children.Add(b);
  
      // Comment this line out when uncommenting Button b block because Triggers 
      // can only be associated with one element
      System.Windows.Interactivity.Interaction.GetTriggers(shape).Add(trigger);
  
      Canvas.SetLeft(shape, _random.Next((int)(shape.Width / 2), (int)(container.Width - (shape.Width / 2))));
      Canvas.SetTop(shape, _random.Next((int)(shape.Height / 2), (int)(container.Height - (shape.Height / 2))));
      container.Children.Add(shape);
  
      return shape;
  }
  </code></pre>
  
  <p>I can see all my RegularPolygons appear on the screen.  I have tested to make sure the path to the sound file works by having other test controls attempt to reference it at the listed location.  No luck getting the EventTrigger with the PlaySoundAction to work.  If I uncomment the lines around button b I can see the button added and Tap it to hear the sound play.</p>
  
  <p>I can get a RegularPolygon to work with a PlaySoundAction EventTrigger in XAML as follows:</p>
  
  <pre><code>&lt;es:RegularPolygon x:Name="haxx" Fill="#FFF4F4F5" Height="100" InnerRadius="1" Canvas.Left="125" PointCount="5" Stretch="Fill" Stroke="Black" Canvas.Top="187" UseLayoutRounding="False" Width="100"&gt;
      &lt;i:Interaction.Triggers&gt;
          &lt;i:EventTrigger EventName="Tap"&gt;
              &lt;eim:PlaySoundAction Source="/Sounds/Correct.mp3"/&gt;
          &lt;/i:EventTrigger&gt;
      &lt;/i:Interaction.Triggers&gt;
  &lt;/es:RegularPolygon&gt;
  </code></pre>
  
  <p><strong>My upcoming solution would be to create a custom control that will let me use the shape of the RegularPolygon with the functionality of the Button as I can get this to work from codebehind.  Is there anything that I'm missing that is preventing me from having this EventTrigger work?</strong></p>