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

Alternative for handling context menu click event in RadTreeView

4 Answers 235 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 01 Feb 2011, 12:49 PM
Hi

My RadTreeView's are in a HiearchicalDataTemplate so the number of TreeViews is not known at design time.

I need to handle the ContextMenu's click event on a RadTreeViewItem but obviously I cannot just write RadTreeView.AddHandler

Does anyone have an alternative idea?

Thanks.

<Grid>
    <Grid.Resources>
            <Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadTreeViewItem}">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            </Style>
             
            <HierarchicalDataTemplate
                x:Key="TreeViewItemTemplate"
                ItemsSource="{Binding TreeViewItemChildren}">
                <StackPanel Orientation="Horizontal">
                    <telerik:RadContextMenu.ContextMenu>
                        <telerik:RadContextMenu>
                            <telerik:RadMenuItem Header="Add to bookmarks" />
                        </telerik:RadContextMenu>
                    </telerik:RadContextMenu.ContextMenu>
                    <Image Margin="0,0,2,0" Height="16" Width="16">
                        <Image.Style>
                            <Style TargetType="Image">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IconType}" Value="Folder">
                                        <Setter Property="Source" Value="{StaticResource Folder}"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IconType}" Value="MultiPubReport">
                                        <Setter Property="Source" Value="{StaticResource MultiPubReport}"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IconType}" Value="SinglePubReport">
                                        <Setter Property="Source" Value="{StaticResource SinglePubReport}"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IconType}" Value="Publication">
                                        <Setter Property="Source" Value="{StaticResource Publication}"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
             
            <HierarchicalDataTemplate x:Key="TreeViewTemplate">
                <telerik:RadTreeView
                    x:Name="ReportsTreeView"                   
                    ItemsSource="{Binding TreeViewItemChildren}"
                    ItemTemplate="{StaticResource TreeViewItemTemplate}"
                    IsLineEnabled="True"
                    IsDragDropEnabled="True"
                    PreviewDragEnded="ReportsTreeView_PreviewDragEnded"                   
                    SelectionMode="Single"
                    ItemContainerStyle="{StaticResource ItemContainerStyle}">
                </telerik:RadTreeView>
            </HierarchicalDataTemplate>
             
            <HierarchicalDataTemplate
                x:Key="PanelBarItemTemplate"
                ItemsSource="{Binding TreeViewItemViewModels}"
                ItemTemplate="{StaticResource TreeViewTemplate}" >
                <TextBlock Text="{Binding Name}"></TextBlock>
            </HierarchicalDataTemplate>
             
        </Grid.Resources>
         
        <telerik:RadPanelBar
            x:Name="ReportMenu"            
            ExpandMode="Multiple"
            ItemTemplate="{StaticResource PanelBarItemTemplate}"
            VerticalAlignment="Top">
        </telerik:RadPanelBar>
             
    </Grid>

4 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 04 Feb 2011, 12:29 PM
Hello Ryan,

Could you please examine the attached approach and let me know if it fits in your scenario? I just introduced the ItemClick event in XAML and it worked. Please let me know if I am missing something.

Best wishes,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Ryan
Top achievements
Rank 1
answered on 07 Feb 2011, 12:12 PM
Thank you Petar, but how do I know which RadTreeViewItem has been clicked?
0
Petar Mladenov
Telerik team
answered on 07 Feb 2011, 03:51 PM
Hi Ryan,

You can use the Opened event of the RadContextMenu and get the clicked RadTreeViewItem behind like so:
private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
       {
           DataItem clicked = (sender as RadContextMenu).GetClickedElement<RadTreeViewItem>().Item as DataItem;
           MessageBox.Show(clicked.Name.ToString());            
       }
You can also find the described approach in the attached solution.
Feel free to ask if you need further assistance.

Kind regards,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Ryan
Top achievements
Rank 1
answered on 07 Feb 2011, 05:32 PM
Great. Thanks very much.
Tags
TreeView
Asked by
Ryan
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Ryan
Top achievements
Rank 1
Share this question
or