get radtreeview item on rightclick

4 posts, 0 answers
  1. Andii
    Andii avatar
    17 posts
    Member since:
    Nov 2006

    Posted 11 Dec 2008 Link to this post

    I have found this rightclick event handler. ContextMenuExtender.
    Then i hook it up to my RadtreeView by this code

     

    ContextMenuExtender contextMenuExtender = new ContextMenuExtender(uxTreeMyPlaylists);

     

    contextMenuExtender.RightClick +=

    new RightClickHandler(contextMenuExtender_RightClick);

     


    then when a rightclick is triggered in my tree im trying to get the RadTreeViewItem that was clicked
    but i cant find a way to find it.  tried with VisualTreeHelper below
    but it dosent work. Can somebody help me?

     

    void contextMenuExtender_RightClick(object sender, RightClickEventArgs e)

     

    {

     

     

    RadTreeView treeViewItem = (RadTreeView)e.Source;

     

     

     

    List<UIElement> hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(e.RelativePoint,e.Source) as List<UIElement>;

     

     

     

     

    }

     




     

    public class ContextMenuExtender

     

    {

     

    public UIElement ExtendedElement { get; private set; }

     

     

    public ContextMenuExtender(UIElement extendedElement)

     

    {

     

    this.ExtendedElement = extendedElement;

     

     

    HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);

     

    }

     

    public event RightClickHandler RightClick;

     

     

    private void OnContextMenu(object sender, HtmlEventArgs e)

     

    {

     

    if (RightClick != null)

     

    {

     

    if (ExtendedElement.IsHitTestVisible)

     

    {

     

    Point clickPoint = new Point(e.OffsetX, e.OffsetY);

     

     

    List<UIElement> hitElements = new List<UIElement>(System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(clickPoint, ExtendedElement));

     

     

    if (hitElements.Count > 0)

     

    {

    e.PreventDefault();

    e.StopPropagation();

    RightClick(

    this, new RightClickEventArgs(ExtendedElement, clickPoint));

     

    }

    }

    }

    }

    }

     

    public delegate void RightClickHandler(object sender, RightClickEventArgs e);

     

     

    public class RightClickEventArgs

     

    {

     

    public UIElement Source { get; private set; }

     

     

    public Point RelativePoint { get; private set; }

     

     

    public Point AbsolutePoint { get; private set; }

     

     

    internal RightClickEventArgs(UIElement source, Point absolutePoint)

     

    {

     

    this.Source = source;

     

     

    this.AbsolutePoint = absolutePoint;

     

     

    this.RelativePoint = GetPosition(Source);

     

    }

     

    public Point GetPosition(UIElement relativeTo)

     

    {

     

    GeneralTransform transform = Application.Current.RootVisual.TransformToVisual(relativeTo);

     

     

    return transform.Transform(AbsolutePoint);

     

    }

    }

  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 15 Dec 2008 Link to this post

    Hello Andreas,

    The code that you provide works like you want it. I just run it in IE7 and I was able to found the RadTreeViewItem in hits list. You should enumerate them and compare the results with RadTreeViewItem (using is).

    Do you get something in hits list?


    Greetings,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Harsh
    Harsh avatar
    5 posts
    Member since:
    Apr 2009

    Posted 19 Sep 2009 Link to this post

    Even I cannot get the element being click. Hits contains nothing, count = 0. Please help
  5. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 22 Sep 2009 Link to this post

    Hi Harsh,

    Is your application windowless?

    Greetings,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top