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

RadTreeView Right Click working in outside of the nodes(Empty space) also

1 Answer 347 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Runjith
Top achievements
Rank 2
Runjith asked on 08 Jul 2014, 05:51 AM
Hi,
I am using Telerik RadTreeView control, I have implemented context menu when i right click for selected treenode,if i right click empty space same context menu displaying.How to remove contextmenu for empty place.

Regards,
Ranjith

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 09 Jul 2014, 11:48 AM
Hello Ranjith,

Without your implementation I cannot be sure what is causing the reported behavior behavior. I prepared a sample project that demonstrates how you can implement context menu for each RadTreeViewItem. Basically you can define the context menu for the UI element placed in the ItemTemplate of the RadTreeView.
<telerik:RadTreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding Title}" HorizontalAlignment="Stretch" ContextMenuOpening="TextBlock_ContextMenuOpening">
            <TextBlock.ContextMenu>                           
                <ContextMenu>
                    <MenuItem>
                        <MenuItem.Header>
                            <TextBlock>
                                <TextBlock Text="Context menu item for tree item with Header: "/>
                                <TextBlock Text="{Binding Title}" />                                                      
                            </TextBlock>                                   
                        </MenuItem.Header>
                    </MenuItem>
                    <MenuItem Header="Item 2" />
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>

If your context menu is defined for the entire treeview, instead for each item, you can subscribe for the ContextMenuOpening event of the RadTreeView and inside the handler check if the item under the mouse is a RadTreeViewItem, if it's not handle the event. This will prevent displaying the context menu.
private void RadTreeView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    var elementUnderMouse = // get item under the mouse cursor position or the RadTreeViewItem which IsMouseOver property is set to True
    if (elementUnderMouse is RadTreeViewItem)
    {
        e.Handled = true;
    }
}

Please give the project a try and let me know if it helps. If not, can you please open a new support ticket and send me an isolated project with your implementation. This will help me in better understanding your scenario and assist you further.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
TreeView
Asked by
Runjith
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Share this question
or