Spontaneous Publicity
blogs are the new phone book

My version of sombody else's code: Finding the Index of an item in a Drag Drop operation

March 6, 2007 16:57 by Luke

While working on an application that requires drag and drop, I ran into trouble when trying to figure out where in the target list (in my case, a ListBox) to insert the dropped item. I ran across Josh Smith's article Drag and Drop Items in a WPF ListView which covers (quite nicely) how to handle drag and drop operations within and between ListViews.  While reading through the code, I found that he uses some code from Dan Crevier which was posted on Lester's Blog. Ok, now that I have taken you through the tangled web of other people's code, I will share how I used the code and changed it a little to work in my situation.

 

public static Point GetMousePosition(Visual relativeTo) { Win32Point mouse = new Win32Point(); GetCursorPos(ref mouse); System.Windows.Interop.HwndSource presentationSource = (System.Windows.Interop.HwndSource)PresentationSource.FromVisual(relativeTo); ScreenToClient(presentationSource.Handle, ref mouse); GeneralTransform transform = relativeTo.TransformToAncestor(presentationSource.RootVisual); Point offset = transform.Transform(new Point(0, 0)); return new Point(mouse.X - offset.X, mouse.Y - offset.Y); } public static int GetIndexUnderDragCursor(ItemsControl control) { int index = -1; for (int i = 0; i < control.Items.Count; ++i) { if (control.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) { DependencyObject item = control.ItemContainerGenerator.ContainerFromIndex(i); if (IsMouseOver(item as Visual)) { index = i; break; } } } return index; } public static bool IsMouseOver(Visual target) { if (target == null) return false; Rect bounds = VisualTreeHelper.GetDescendantBounds(target); Point mousePos = GetMousePosition(target); return bounds.Contains(mousePos); }

I didn't change the GetMousePosition method at all from what I found on Lester's blog.  Then I slightly modified some of Josh's code so that it is a little more general.  I hope this helps.  Suggestions welcome.


Tags:
Categories:
Actions: E-mail | del.icio.us | Permalink | Comments (46) | Comment RSSRSS comment feed

Comments

March 9. 2007 04:55

Josh Smith


          You might want to consider using the modified version of Dan Crevier's GetMousePosition method, as seen in my article.  If you are running an app on a machine whose monitor has better resolution than the standard 96 dpi, you'll encounter problems with Crevier's implementation.  His code doesn't take into account the fact that WPF works with logical pixels.

          Josh
        

Josh Smith

March 9. 2007 05:03

Luke Foust

Thanks Josh.  I'll check it out.

Luke Foust

April 26. 2007 10:09

emlak

very nice topic thnkss

emlak

July 28. 2010 21:05

natural cures for bacterial vaginosis

Tremendous blog post, a bunch of high quality info.  I am about to show my friends and ask them the things they think.

natural cures for bacterial vaginosis

July 31. 2010 19:37

computerdude2002

I am really not too familiar with this subject but I do like to visit blogs for layout ideas and interesting topics. You actually expanded upon a subject that I usually don't care much about and made it very exciting. This is a memorable blog that I will take note of. I already bookmarked it for future reference. Thx

computerdude2002

August 4. 2010 00:50

Glass pods

Ha, can't say you're wrong. Just need to get my head around this.

Glass pods

August 5. 2010 05:39

abercrombie

Do not share completely the post, but the argument is well reputable. Congratulations to the creator of the weblog

abercrombie

August 6. 2010 14:28

webcamsex

good references, there were similar discussions on other forums. I'll watch this topic.. <a href="http://www.naughtywebcamsex.com" title="Webcam Girls">Webcam Girls</a>

webcamsex

August 6. 2010 18:00

live sex

yes, quite interesting. I still watch this topic.. <a href="http://www.naughtywebcamsex.com" title="Webcam Girls">Webcam Girls</a>

live sex

August 10. 2010 16:05

Consuelo Babey

This is the 2nd instance I've discovered your web sites in the last couple weeks.  Seems like I should bookmark it.

Consuelo Babey

August 10. 2010 16:31

Darrin Haseman

Good Evening,
I don't know about that... I hardly think I disagree with that statement.

Darrin Haseman

August 13. 2010 16:39

free link directory

I found your blog last week and started follow your posts religiously. I have not commented on any blog just yet but I was thinking I would like to. It's very exciting to actually contribute to a article even if it's only a blog. I really don't know exactly what to write other than I genuinely enjoyed reading through 8 of the articles. Nice articles indeed. I sure will keep visiting your blog regularly. I learned quite a bit from you. Thanks a bunch!

free link directory

August 15. 2010 14:40

Krystle Waggener

Extremely interesting post thanks for writing it I have added your site to my favorites and will check back  By the way this is a little off topic but I really like your web page layout

Krystle Waggener

August 16. 2010 06:31

Free Apple iPad

My friend told me about this blog so I had to have a look for myself, Myself like my friend also think this is a very good blog, thank you.

Free Apple iPad

August 17. 2010 22:43

dvd ripping software

I wanna to learn programming. It is interesting.

dvd ripping software

August 17. 2010 22:52

usb data transfer cable

Hi, i must say fantastic blog you have, i stumbled across it in Google. Does you get much traffic?

usb data transfer cable

August 21. 2010 15:19

Fat Loss 4 Idiots

mmmm, I think you may be right in what you have said in this article. Whoever there are many people will disagree with your opinion.

Fat Loss 4 Idiots

August 21. 2010 21:26

flip ultrahd camcorder

I have been reading your articles during my lunch break, and I have to admit the whole article has been very enlightening and very well written. I thought I would let you know that for some reason this blog does not view well in Internet Explorer 8. I wish Microsoft would stop changing their software. I have a question for you. Do you mind swapping blog roll links? That would be really cool!

flip ultrahd camcorder

August 22. 2010 16:35

Myesha Morter

Good blog! I have been looking for this information all around. This is was I looking for.

Myesha Morter

August 24. 2010 13:57

loan origination

Before when I was on this site, I was mindlessly tracing by what you wrote, Also I even told my buds because these guys always begin conversations and begin arguing all the time.  

loan origination

August 27. 2010 00:18

Dorian Crary

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.

Dorian Crary

August 27. 2010 12:26

botkier

I am speechless. This is a great blog and very engaging too. Great work! That's not really much coming from an amateur blogger like me, but it's all I could say after enjoying your posts. Excellent grammar and vocabulary. Not like other blogs. You really know what you're talking about too. So much that you made me want to explore more. Your blog has become a stepping stone for me, my friend. Thanks for the detailed journey. I really enjoyed the 27 posts that I have read so far.

botkier

August 28. 2010 02:00

Randell Byczek

why not posting more?

Randell Byczek

August 28. 2010 17:54

Otis Volkmann

do you have an rss feed? I want to add it to my reader but I can't find it...

Otis Volkmann

August 29. 2010 08:23

seduction affiliate

hey, with so many comments you should add an affiliate program to your blog and make some money...

seduction affiliate

August 29. 2010 16:36

facesitting

I have been reading your posts during my lunch break, and I have to admit the whole article has been very valuable and very well written. I thought I would let you know that for some reason this blog does not view well in IE 8. I wish Microsoft would stop changing stuff. I have a question for you. Would you mind swapping blog roll links? That would be really cool!

facesitting

August 29. 2010 19:37

Darryl Giacone

I bookmarked your blog but I hope you will post more...

Darryl Giacone

August 29. 2010 20:58

student driver magnet

Good Post, I am a big believer in writing comments on blogs and forums to inform the blog writers know that they’ve added some thing useful to the world wide web!

student driver magnet

August 30. 2010 18:42

bail bonds baltimore

One of the more impressive blogs Ive seen.  Thanks so much for keeping the internet classy for a change.  Youve got style, class, bravado.  I mean it.  Please keep it up because without the internet is definitely lacking in intelligence.

bail bonds baltimore

August 30. 2010 22:30

Filiberto Lundin

are you using a custom template?

Filiberto Lundin

August 31. 2010 00:06

payroll services

Have you considered about introducing some social bookmarking buttons to these blog posts. At least  for facebook.

payroll services

August 31. 2010 02:41

Berta Dobrinin

Considerably, the post is actually the freshest on that worthy topic. I concur with your conclusions and definitely will eagerly look forward to your incoming updates. Saying thanks will not simply just be sufficient, for the fantasti c lucidity in your writing. I definitely will instantly grab your rss feed to stay informed of any updates. Pleasant work and also much success in your business efforts!

Berta Dobrinin

August 31. 2010 05:54

jokes

nice blog! keep the good work!

jokes

August 31. 2010 16:37

garmin refurbished

All I can say is... please write more. OMG, it seems as though I watched a video instead of reading an article...it's so crystal clear. You totally know what you're talking about. My thought was,  why waste your intelligence on blog articles when you could be authoring books? Think about it. You're awesome at what you do. WORD!

garmin refurbished

August 31. 2010 18:26

dating tips blog

good work! keep posting more. I have a dating blog too but it's not so popular like yours..

dating tips blog

August 31. 2010 22:28

pine overmantle mirror

Comfortabl y, the article is in reality the sweetest on that notable topic. I concur with your conclusions and will certainly eagerly look forward to your coming updates. Just saying thanks can not simply be enough, for the fantasti c clarity in your writing. I can correct away grab your rss feed to stay privy of any updates. De lightful work and much success in your business endeavors!

pine overmantle mirror

September 1. 2010 20:37

ETFs

it will be great if you post more...

ETFs

September 2. 2010 15:28

gift ideas

try to change your template, there are some great ones...

gift ideas

September 2. 2010 16:50

Nelson Rollheiser

i'm adding your blog rss feed so that i can see your new posts.  keep up the good work!

Nelson Rollheiser

September 2. 2010 22:55

Porter Eppinette

You can find certainly quite a lot of details just like that to take into consideration. That´s a great point to bring up. I provide the thoughts above as general inspiration but clearly there are actually questions just like the one you bring up exactly where the most necessary point definitely will be working in honest excellent faith. I don´t know if greatest practices have emerged around things like that, but I am certain that your task is clearly identified as a fair game.

Porter Eppinette

September 3. 2010 05:26

ETF newsletters

any plan to update this blog?

ETF newsletters

September 3. 2010 21:31

mixed wrestling

I discovered your blog last week and started follow your posts regularly. I have not commented on any blog just yet but I was contemplating I would like to. It is really exciting to actually contribute to a publication even if it's only a blog. I really don't know exactly what to say other than I actually enjoyed reading through 2 of the articles. Nice posts indeed. I sure will keep visiting your blog weekly. I learned quite a bit from you. Thx!

mixed wrestling

September 5. 2010 08:50

ETFs

do you have a different blog where you post more often?

ETFs

September 7. 2010 04:25

Carpet Cleaning Philadelphia

I value the article post.Thanks Again. Keep writing.

Carpet Cleaning Philadelphia

September 9. 2010 00:27

profile backlinks

As an amateur writer from Canada, I seldomly come across really quality publications like your blog which really helps me inspirationally. It shows me what excellent writings are all about. There are tons of substandard writings on the web and it's very sad. Thank you for your well written posts! I frequently read your blog and enjoy the content you share. I look forward to more of your work.

profile backlinks

September 9. 2010 07:24

KimK

are you a Kim fan?

KimK

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading