How to set a different click handler on each button in a HierarchicalDataTemplate

2 posts, 0 answers
  1. Simon
    Simon avatar
    47 posts
    Member since:
    May 2008

    Posted 15 Sep 2011 Link to this post


    I've created a HierarchicalDataTemplate for my items collection which is bound to a business object.
    The TemplateSelector chooses which template to use programmatically. 
    This all works fine.

    The DataTemplate code looks like this...
    <telerik:HierarchicalDataTemplate telerik:ContainerBinding.ContainerBindings="{StaticResource TreeViewNodeBindings}"
                                      ItemsSource="{Binding Items}">
        <StackPanel Orientation="Horizontal">
            <telerik:RadButton x:Name="EditTreeListDataButton"
                               Click="EditListButton_Click" />
            <Image Stretch="None"
                   Margin="2" />
            <TextBlock Text="{Binding Header,Mode=OneWay}"
                       FontSize="12" />

    I would like to know how I can have a different click delegate for each button that is shown for the items collection.
    Each button repeats per item so all the click events point to the same code. I want each click event to be unique. 
    Obviously if I knew which item the button belonged to, I could run the appropriate code, but nothing I've tried works.
    How do I do this? I'm sure it's a common problem.

    Thanks Simon
  2. Petar Mladenov
    Petar Mladenov avatar
    2882 posts

    Posted 20 Sep 2011 Link to this post

    Hi Simon,

     Yes, finding the parent RadTreeViewItem of the Clicked Button is a typical approach. In the Clicked event handler you can use the telerik's extension method : Telerik.Windows.Controls.ParentOftype<RadTreeViewitem.(). On the other hand, if you implement an MVVM scenario, you could consider using Commands. Every RadTreeViewItem could have its own corresponding ViewModel and the Button could be bound to a Command in this ViewModel class.
    Please let us know if you need more info.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top