Alternative for handling context menu click event in RadTreeView

5 posts, 0 answers
  1. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 01 Feb 2011 Link to this post

    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>
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 04 Feb 2011 Link to this post

    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>>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 07 Feb 2011 Link to this post

    Thank you Petar, but how do I know which RadTreeViewItem has been clicked?
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 07 Feb 2011 Link to this post

    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>>
  6. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 07 Feb 2011 Link to this post

    Great. Thanks very much.
Back to Top
UI for WPF is Visual Studio 2017 Ready