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

Right click to select item

11 Answers 699 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 2
Martin asked on 23 Apr 2009, 03:28 PM
Hi,

I need to be able to select items on right click so that the context menu functions can look at SelectedItem and get the right result. I looked at using the ContextMenuOpening event but this doesnt show which item the mouse was over when triggering the event.

Any suggestions will be greatly appreciated thanks.

p.s. When is dragging items between multiple treeviews likely to be available?

Thanks,
Martin

11 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 26 Apr 2009, 11:41 AM
Hi Martin,

You can add a Class EventHandler that will select an item on MouseRight click like so:

static Window1()  
{  
    EventManager.RegisterClassHandler(typeof(RadTreeViewItem),   
        Mouse.MouseDownEvent, new MouseButtonEventHandler(OnTreeViewItemMouseDown), false);  
}  
 
public static void OnTreeViewItemMouseDown(object sender, MouseButtonEventArgs e)  
{  
    var treeViewitem = sender as RadTreeViewItem;  
    if (e.RightButton == MouseButtonState.Pressed)  
    {  
        treeViewitem.IsSelected = true;  
        e.Handled = true;  
    }  

As for your second question:

Currently, you can implement drag drop across TreeViews using the standard DragDrop in WPF but there is very little help on the internet on how to use it.

We are planning to port our Silverlight DragDrop for WPF for the Q2 release of the controls, then you will be able to use drag/drop across controls.

Hopefully this will work for you,

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Prash
Top achievements
Rank 1
answered on 05 Jun 2009, 04:11 PM
I tried the code that you have given earlier, It works fine but I am using the template in the treeview

<telerik:RadTreeView.ItemTemplate x:Uid="TreeItemTemplate" >
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" x:Name="dataTemplate" >
                    <StackPanel Orientation="Horizontal" >
                        <Image x:Name="ImgTreeNode" Height="16" Width="16" Margin="0,0,2,0"
                               Source="{Binding Path=ImageKey}"/>
                        <WrapPanel>
                            <WrapPanel>
                                <Image x:Name="ImgTreeNodeSeverity"  Margin="0,0,2,0" Height="16" Width="16"
                                    Stretch="UniformToFill" StretchDirection="DownOnly" HorizontalAlignment="Left"
                                   Source="{Binding src" ClipToBounds="True" SnapsToDevicePixels="True" />
                                </WrapPanel>
                                    <!--Loaded="ImgTreeNode_Loaded"-->
                            <TextBlock x:Name="txtObjName" Text="{Binding DisplayName}"/>
                        </WrapPanel>
                    </StackPanel>
                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding src}" Value="FaultNormal">
                            <Setter TargetName="ImgTreeNodeSeverity" Property="Image.Source" Value="{Binding null}"/>
                            <Setter TargetName="ImgTreeNodeSeverity" Property="Image.Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </telerik:RadTreeView.ItemTemplate>



Now when i use the code

static Window1()  
{  
    EventManager.RegisterClassHandler(typeof(RadTreeViewItem),   
        Mouse.MouseDownEvent, new MouseButtonEventHandler(OnTreeViewItemMouseDown), false);  
}  
 
public static void OnTreeViewItemMouseDown(object sender, MouseButtonEventArgs e)  
{  
    var treeViewitem = sender as RadTreeViewItem;  
    if (e.RightButton == MouseButtonState.Pressed)  
    {  
        treeViewitem.IsSelected = true;  
        e.Handled = true;  
    }  
}

I get this event only when i click on the Images or the stack panel(refer to the xaml code), If I click on the blank space i dont get the
treeViewitem  i.e. it is null and i cant show the context menu there.

I am not able to use the contextMenuOpening as the break point there is never hit.

Please suggest the solution. Any code will be helpful.
Thanks and regards
Prashant
0
Kiril Stanoev
Telerik team
answered on 09 Jun 2009, 06:55 AM
Hi Prash,

I have created a sample project according to the HierarchicalDataTemplate you have provided. I did not experience any issues while testing the project.
Please let me know if there is something I am missing.

Kind regards,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Prash
Top achievements
Rank 1
answered on 24 Jun 2009, 08:23 AM

The shared sample is working fine.

 

But now we are facing the issue while using tree control in the two different windows (different class files)of the same application.

Issue: Let say "Window1" and "Window2" are two windows (different class files) and I want to show the context menu on the type of item selected in the tree control (treeViewItem) of "Window1" and dont want to show context menu on "Window2". Now as we have registered the "MouseDownEvent" on tree view item

"EventManager.RegisterClassHandler(typeof(RadTreeViewItem),

                                                Mouse.MouseDownEvent, new MouseButtonEventHandler(OnTreeViewItemMouseDown), false);"

in "Windows1", "OnTreeViewItemMouseDown" function will get called when mouse is down on tree view item contained in "Window1". But this function will also get called when mouse is down on tree view item of "Windows2".

 

How can we registere the mouseDown event of tree view item  for "Windows1" only?

0
Valentin.Stoychev
Telerik team
answered on 24 Jun 2009, 01:51 PM
Hi Prash,

You can inspect the ParentTreeView property of the treeview item that fired the event and to execute your handler code only if the treeview is in the appropriate UserControl.

Will this work for you?

Greetings,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
William
Top achievements
Rank 1
answered on 29 Jul 2009, 09:51 PM
Hi,
I'm also working with a right click selection problem.  If I use your code(even the sample) when you right click an item I would like to have the focus (selection) select the new right clicked item and remove focus from the old left clicked item.  How can I do that?  Thanks
0
Miroslav
Telerik team
answered on 30 Jul 2009, 06:41 AM
Hello William,

have you tried explicitly focusing the item, like so:

treeViewitem.IsSelected = true;  
treeViewitem.Focus(); 

I tried this with the code from the class event handler.

Does this work in your case?

Sincerely yours,

Miroslav

the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.

0
William
Top achievements
Rank 1
answered on 30 Jul 2009, 11:58 AM
That works, but it shows another little problem,  Once you click either (in your sample) new or delete the action occurs and the then the menu disappears however the hover is always over a different treeview item.  Is there an easy way to turn off the hover highlight for a few seconds?  Or turn it off and then turn it back on at the next treeview click?
Thanks
0
Miroslav
Telerik team
answered on 03 Aug 2009, 10:39 AM
Hello William,

The mouse hover cannot be switched on / off since it is implemented as a Trigger for the IsMouseOver property of the visual element.

If you need another item to be appear highlighted then you can try selecting it, but this is not exactly the same as highlighting.

I am not sure I understood you fully, could you please clarify a bit why you need to turn on/off the highlight.

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
thomas kuang
Top achievements
Rank 1
answered on 12 Jul 2010, 08:24 AM

 Hello,my friend.Now, I hava a big trouble when I use above code , this RadTreeview control selected many item when I  right click many defferent item.you will clear when you look the attach files.Please give me reply as soon as possible. Thanks my friend.

UI code:

<Telerik:RadTreeView Name="_treeView" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" SelectionMode="Extended" ExpanderStyle="{StaticResource Expander}"  >
            <telerikNavigation:RadContextMenu.ContextMenu >
                <telerikNavigation:RadContextMenu  x:Name="radTreeViewMenu">      
                    <telerikNavigation:RadMenuItem Header="Add Division" Tag="Add Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Copy Division" Tag="Copy Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Rename Division" Tag="Rename Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Delete Division" Tag="Delete Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Compose Division" Tag="Compose Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Show PDF AT Division" Tag="Show PDF AT Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Show Clean PDF At Division" Tag="Show Clean PDF At Division" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Job Ticket" Tag="Job Ticket" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Add Job" Tag="Add Job" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Rename Job" Tag="Rename Job" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Delete Job" Tag="Delete Job" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Compose Job" Tag="Compose Job" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Show PDF" Tag="Show PDF" IsEnabled="False"/>
                    <telerikNavigation:RadMenuItem Header="Show Clean PDF At Job" Tag="Show Clean PDF At Job" IsEnabled="False"/>
                </telerikNavigation:RadContextMenu>
            </telerikNavigation:RadContextMenu.ContextMenu>
        </Telerik:RadTreeView>
I have solve this problem.Thanks my friend.
0
Kiril Stanoev
Telerik team
answered on 15 Jul 2010, 08:27 AM
Hello Thomas,

This is because you have set the SelectionMode to Extended and you are probably setting the selection in code behind. However, if you want to have only one selected item on right click you have to deselect the previously right clicked item. I've attached a sample project demonstrating this functionality. Have a look at it and let me know if it helps.

All the best,
Kiril Stanoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
Martin
Top achievements
Rank 2
Answers by
Miroslav
Telerik team
Prash
Top achievements
Rank 1
Kiril Stanoev
Telerik team
Valentin.Stoychev
Telerik team
William
Top achievements
Rank 1
thomas kuang
Top achievements
Rank 1
Share this question
or