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

get radtreeview item on rightclick

3 Answers 135 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 11 Dec 2008, 11:14 PM
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);

 

}

}

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 15 Dec 2008, 11:49 AM
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.
0
Harsh
Top achievements
Rank 1
answered on 19 Sep 2009, 11:41 PM
Even I cannot get the element being click. Hits contains nothing, count = 0. Please help
0
Valentin.Stoychev
Telerik team
answered on 22 Sep 2009, 10:37 AM
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.
Tags
TreeView
Asked by
Andreas
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Harsh
Top achievements
Rank 1
Valentin.Stoychev
Telerik team
Share this question
or