Right click to select item

12 posts, 0 answers
  1. Martin
    Martin avatar
    17 posts
    Member since:
    Apr 2009

    Posted 23 Apr 2009 Link to this post

    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
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 26 Apr 2009 Link to this post

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

    Posted 05 Jun 2009 Link to this post

    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
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 09 Jun 2009 Link to this post

    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.
  6. Prash
    Prash avatar
    27 posts
    Member since:
    Jan 2009

    Posted 24 Jun 2009 Link to this post

    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?

  7. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 24 Jun 2009 Link to this post

    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.
  8. William
    William avatar
    55 posts
    Member since:
    Sep 2008

    Posted 29 Jul 2009 Link to this post

    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
  9. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 30 Jul 2009 Link to this post

    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.

  10. William
    William avatar
    55 posts
    Member since:
    Sep 2008

    Posted 30 Jul 2009 Link to this post

    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
  11. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 03 Aug 2009 Link to this post

    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.
  12. thomas kuang
    thomas kuang avatar
    4 posts
    Member since:
    May 2010

    Posted 12 Jul 2010 Link to this post

     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.
  13. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 15 Jul 2010 Link to this post

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready