This is a migrated thread and some comments may be shown as answers.

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

1 Answer 57 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 15 Sep 2011, 07:46 AM
Hi,

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"
                           Content="..."
                           Click="EditListButton_Click" />
        <Image Stretch="None"
               Source="../Resources/treeview_icon_small.png"
               VerticalAlignment="Center"
               Margin="2" />
        <TextBlock Text="{Binding Header,Mode=OneWay}"
                   VerticalAlignment="Center"
                   Margin="2"
                   FontSize="12" />
    </StackPanel>
</telerik:HierarchicalDataTemplate>


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

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 20 Sep 2011, 09:57 AM
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 >>

Tags
TreeView
Asked by
Simon
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or