title: dynamic datatemplates silverlight events
body: <p>Looking for information on how to hook events to DataTemplate for silverlight using code only. This will crash my browser when rendered.</p>
  
  <pre><code>private DataTemplate Create(Type type)        
  {            
    DataTemplate dt = new DataTemplate();            
    string xaml = "&lt;DataTemplate x:Name='dt' xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:basics='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls'  &gt;"
                  + "&lt;Border Height='135' Width='110' HorizontalAlignment='Left' VerticalAlignment='Top' Background='#7FFCFAFA' CornerRadius='5,5,5,5'&gt;"
                  + "&lt;StackPanel Orientation='Vertical' Width='110' Height='135'&gt;"
                  + "&lt;Image Source='{Binding Path=ThumbnailPath}' Width='64' Height='64' Margin='5'  HorizontalAlignment='Left' /&gt;"
                  + "&lt;TextBlock Text='{Binding Path=Title}' Margin='5' FontFamily='Verdana' HorizontalAlignment='Left' TextWrapping='Wrap' Foreground='#FFFAF8F8' Width='100' FontSize='9' /&gt;"
                  + "&lt;TextBlock Text='{Binding Path=FileDuration}' Margin='5' FontFamily='Verdana' HorizontalAlignment='Left' Foreground='#FFFAF8F8' Width='100' FontSize='9'/&gt;"
                  + "&lt;StackPanel Orientation='Horizontal' Background='{x:Null}' x:Name='GridMenuBar'&gt;"
                  + "&lt;Button ClickMode='Press' x:Name='play_buttons' HorizontalAlignment='Left' Template='{StaticResource GlassPlay}'  BorderBrush='{x:Null}' Foreground='#FFF9F7F7' Background='{x:Null}' /&gt;"
                  //+ "&lt;Button Height='Auto' HorizontalAlignment='Left' VerticalAlignment='Top' Width='Auto' Content='Button' x:Name='addPlayListItemButton' Template='{StaticResource GlassAddPlaylist}' Click='addPlayListItemButton_Click' /&gt;"
                  + "&lt;/StackPanel&gt;"
                  + "&lt;/StackPanel&gt;"
                  + "&lt;/Border&gt;"
                  + "&lt;/DataTemplate&gt;"; 
      dt = (DataTemplate)XamlReader.Load(xaml);
      return dt;
  }
  </code></pre>