开发者

MouseEnter/MouseLeave and mouse left button down

I'am tr开发者_如何学Goying to implement a click and drag selection like the one in windows explorer (the blue selection rectangle that occurs when you keep the mouse button down and you move the mouse).

So basically I have a ListView, with styled and templated ListViewItem. I have added MouseEnter and MouseLeave event on my ListViewItem (with the EventSetter), It works fine except when the left mouse button is down. In this case, the events doesn't get fired, which is not good for what I'm trying to achieve.

Do you know if there is any good workaround for this, I want to know when I'm over an item or not when the left mouse button is down.

For now I've tried with the VisualTreeHelper.HitTest(), but I only know when I cross a ListViewItem, and I need to know when I leave it.

Thank you.


You have to capture the mouse UIElement.CaptureMouse when the button is pressed and then release the mouse UIElement.ReleaseMouseCapture during the MouseUp event. You do not have to worry about any other events because all mouse input goes to your UIElement during the capture.

This form of selection is called "lasso select" and most widgets in Windows apps support it. In fact, the ListView itself already does.


Even if your MouseEnter and Leave events were fired that would not provide a good method for selection because both events are irrelevant in the end: It does not matter if your mouse ever touched that folder or file, all that matters is if it is in the rectangle at the point in time when you release the button. This means that Mouse Up & Down should be enough. Depending on your list you might even be able to infer which items should be selected just from looking at the two Items on which those events occurred (e.g. if it's just a one-dimensional list as opposed to a two-dimensional grid).

If you have a grid you'd need more complex mechanics like keeping track of which area is covered and checking which items are inside or on the edge.


So I have found a different solution to handle this behavior.

I've started, with this stackoverflow answer Click and drag selection box in WPF

In the mouseMove, after modifying the selectionBox size, I select the items that are in the selectionBox region.

I do it this way :

 //Select all visible items in select region.
 Rect selectRect = new Rect(Canvas.GetLeft(selectionBox), Canvas.GetTop(selectionBox),
                (Canvas.GetLeft(selectionBox) + selectionBox.Width), (Canvas.GetTop(selectionBox) + selectionBox.Height));

 RectangleGeometry rr = new RectangleGeometry(selectRect);
 foreach (CustomElement elt in mainList.Items)
 {
  ListViewItem item = mainList.ItemContainerGenerator.ContainerFromItem(elt) as ListViewItem;
  Rect r = LayoutInformation.GetLayoutSlot(item);
  if (r.IntersectsWith(selectRect))
        item.IsSelected = true;
  else
        item.IsSelected = false;
 }

I've found that LayoutInformation can gives you the Rect that represent your object, so I can check if it intersect with the selectionBox Rect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜