title: Silverlight: Define event handler in hierarchical data template
body: <p>I am having problems getting at a click event of a button and am using Silverlight 3.0 w/ matching <a href="http://www.codeplex.com/Silverlight" rel="nofollow noreferrer">Silverlight Toolkit</a>.</p>
  
  <h1>Problem</h1>
  
  <p>I have this TreeView:</p>
  
  <p><a href="http://a.imagehost.org/0939/TreeView.png" rel="nofollow noreferrer">TreeView sample http://a.imagehost.org/0939/TreeView.png</a>.</p>
  
  <p>The value for a certain node is the sum of the values of its children. Only in leaves can data be added (for the time being).
  What I want to achieve is that a user can add (and eventually remove) entries in the tree to eventually create a custom diagram.
  To that end I would like the "plus sign" to insert a new line / node as child of the node where the user clicked. (I.e. if I click the plus at "Display", I get a line below to specify CRT or TFT or whatever.)</p>
  
  <p>Thing is, for all my brain is worth, I don't know how to receive any useful event.
  The TextBlock, TextBox and Button are defined in a hierarchical template and I can't define a Click-handler in that template.</p>
  
  <p>OTOH, I haven't found a way to get at the template items of a certain TreeViewItem from within (c#) code. Very well am I able to do trv.ItemContainerGenerator.GetContainerFromItem(item), and as <a href="http://silverlight.net/blogs/justinangel/archive/2009/05/19/silverlight-treeview-advanced-scenarios-treeviewextended.aspx" rel="nofollow noreferrer">Justin Angel showed</a> I can very well change the font size, but didn't find any way to access the textbox or button.</p>
  
  <p>Is there any way to capture the click event to the button? Or any alternative way of getting something that gives the "add below" functionality?</p>
  
  <p>Thank you in advance.</p>
  
  <hr>
  
  <h1>More Data</h1>
  
  <p>The treeview xaml is this:</p>
  
  <pre><code>&lt;controls:TreeView x:Name="SankeyDataTree" 
      ItemTemplate="{StaticResource SankeyTreeTemplate}" BorderThickness="0" 
      Background="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Top"&gt;
      &lt;controls:TreeViewItem IsExpanded="True"&gt;
          &lt;controls:TreeViewItem.HeaderTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock Text="Loading..."/&gt;
              &lt;/DataTemplate&gt;
          &lt;/controls:TreeViewItem.HeaderTemplate&gt;
      &lt;/controls:TreeViewItem&gt;
  &lt;/controls:TreeView&gt;
  </code></pre>
  
  <p>I use this HierarchicalDataTemplate (and stole the appraoch from <a href="http://geekswithblogs.net/tkokke/archive/2009/06/29/styling-a-treeview-in-silverlight-and-blend-3.aspx" rel="nofollow noreferrer">Timmy Kokke</a>):</p>
  
  <pre><code>&lt;Data:HierarchicalDataTemplate x:Key="SankeyTreeTemplate" ItemsSource="{Binding Children}"&gt;
      &lt;Grid Height="24"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;!-- ... --&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;TextBlock Text="{Binding Path=Value.name, Mode=TwoWay}" VerticalAlignment="Center"/&gt;
          &lt;TextBox Text="{Binding Path=Value.flow, Mode=TwoWay}" Margin="4,0" VerticalAlignment="Center" d:LayoutOverrides="Width" Grid.Column="1" TextAlignment="Right" Visibility="{Binding Children, Converter={StaticResource BoxConverter}, ConverterParameter=\{box\}}"/&gt;
          &lt;TextBlock Text="{Binding Path=Value.throughput, Mode=TwoWay}" Margin="4,0" VerticalAlignment="Center" d:LayoutOverrides="Width" Grid.Column="1" TextAlignment="Right" Visibility="{Binding Children, Converter={StaticResource BoxConverter}, ConverterParameter=\{block\}}"/&gt;
          &lt;Button Margin="0" Grid.Column="2" Style="{StaticResource TreeViewItemButtonStyle}"&gt;
              &lt;Image Source="../Assets/add.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
          &lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Data:HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p>To this TreeView is bound a "<a href="http://dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/" rel="nofollow noreferrer">SimpleTree</a>", whose Values hold basically onle a string (name) and two doubles (flow and throughput).</p>
  
  <pre><code>public String name { get; set; }
  public Double flow { get; set; }
  public Double throughput { get; set; }
  </code></pre>
  
  <p>(Plus the code for the INotifyPropertyChanged to get a twoway bind to the text boxes.)</p>