A New Custom WPF Panel: SpanningStackPanel

March 13, 2007

Nick Thuesen has just published a new custom WPF panel that functions like a stack panel but takes up the entire space provided to it. I love to see new custom panels come out. I think there should be a Codeplex project for custom WPF panels. Maybe if I write any clever ones of my own I will start one.


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

March 6, 2007

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.