Now, i have two listboxes,i want to draw a line from a listboxitem of this listbox to a listboxitem of another listbox. the two listboxitem isn't horizontal if possible. How to get listboxitem coordinate???
-
2http://stackoverflow.com/questions/4095252/how-to-get-xy-coordinates-of-a-control-at-runtime-in-wpf – KyorCode Aug 24 '12 at 07:23
2 Answers
This Code worked for me (here I get the coordinates of the selected item relative to its hosted window):
object selectedEntry = (object)myListBox.SelectedItem;
ListBoxItem lbi = this.myListBox.ItemContainerGenerator.ContainerFromItem(selectedEntry) as ListBoxItem;
Point p = lbi.TranslatePoint(new Point(0, 0), Window.GetWindow(lbi));

- 5,984
- 2
- 17
- 30
First you have to get both items you want to connect. If you have both items you can start calculating the points. I would look for a parent Panel of both listboxes and calculate the points relative to that Panel.
As example you create Grid within two listboxes. Now you just have to calculate the points of both items. Now add a Line to the Grid that contains the two calculated points (point1 = x1, y1 and point2 = x2, y2).
But remember. It is not as easy as it seems because if you scroll you have to update the points. And exactly at that point the next problem apears. If you scroll out view (the items you want to connect) the line will be still visible. So you have to calculate if the line is visible or not...
The best way would be creating a DataGrid and connecting two cells because it is still easier than two different listboxes.

- 5,918
- 3
- 47
- 86