I have a Rectangle within a Canvas. The rectangle has the MouseDragElementBehavior attached to it, so it can move freely within the canvas.
I need to do certain calculations when the smallest changes happen in the position of the rectangle, but i can not find a single best event.
So far I have resorted to MouseMove event for the rectangle which is an overkill. Any alternative?
You should base on the events from MouseDragElementBehaviour class, DragBegun, DragFinished and Draggging.
Related
I have a WPF app with a canvas inside a ScrollViewer. On the canvas I have some rectangles that I drag along a timeline (left-right). This works fine with a mouse, but on a touch screen I get weird behavior if the canvas is wider than the main form. First, when you begin to drag the rectangle, the canvas pans until it has scrolled to the limit, then the rectangle starts to move. I doesn't do this when using a mouse.
Another strange this is that if I pan/drag the canvas (using touch) to the limit of the scrollviewer, the main form compresses by 20-50 pixels on the side opposite of the pan direction. It springs back to shape as soon as you stop dragging. What's going on here and how do I disable this?
It seems that this has to do with ManipulationBoundaryFeedback, but I don't understand exactly how...
EDIT:
So I was able to get a little further by setting the scrollviewer panningmode to PanningMode.None in the rectangle TouchDown handler and then setting it back to PanningMode.Both in the TouchUp handler. This solved the problem of the canvas panning to the limit before the rectangle would move.
Well, I finally got my app to work with touch. As I mentioned above, setting the PanningMode to None on the TouchDown event eliminated the problem with the ScrollViewer. Setting it back to Both in the TouchUp event re-enabled it. What this means is that I can't drag the rectangle past the point where the ScrollViewer would start panning, but in my case this was not a problem.
I also had some issues with the MouseLeftButtonDown event firing during the TouchMove event so I removed it during TouchDown
-= new MouseButtonEventHandler(drag_start)
and re-added it back during TouchUp to eliminate the glitchy behavior.
+= new MouseButtonEventHandler(drag_start)
Maybe this will help someone in the future...
Is there any simple way to move picture parts (any stuff created with DrawImage or DrawString)?
My smart idea:
Override DrawImage(DrawString) method adding ability to store width, height, left, top, of drawn image (and may be boolean variable 'sizable' if I use DrawImage method) to a list of objects. Attach function "WhatToMove" to PictureBox's MouseMove event, which walks through a list of stored rectangle objects, detects what rectangle cursor is over, in case of resizeable rectangle detect if cursor is over any of rectangle's borders and pass this stuff as parameters to the MouseDown function, when I click. Then MouseDown function will attach new function "MoverResizer" to MouseMove event, which redraws picture box with moved or resized parts of image, updates proper rectangle object in list, and also detachs "WhatToMove" from MouseMove. On MouseUp and Leave events of PictureBox detach "MoverResizer" and attach "WhatToMove".
If you think this idea can live you will really help me if tell me how to override DrawImage.
My dumb idea:
Create Labels without text and with borders and locate them over PictureBox right over proper image parts. Then make them draggable and sizeable with their own events. Then translate positions of labels to positions of parts of picture and redraw picturebox. This is simply but I think not so perfect as method above.
I have a Rectangle nested within a Canvas. The rectangle has a MouseDragElementBehavior attached to it, so it can move freely in the canvas.
I need to calculate the position of the rectangle after each drag. The problem is that the
Canvas.GetTop(rectangle1)
only works for the first time, i.e. before the rectangle is dragged. After the drag, the method call returns the initial position.
Those behaviors usually work by applying a RenderTransform, if it uses a TranslateTransform you can get the offset value from that and add it to the canvas position.
Alternatively you might just want to implement your own dragging logic.
I'm trying to drag and drop (slide) a Silverlight element from one part of a window to another.
I've implemented the MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp event handlers on the element, but I've run into a bit of a problem.
All of the examples I've seen involve moving the element by setting the Canvas.Left and Canvas.Top properties. None of the elements I'm trying to manipulate live inside a Canvas. Is there a way to set the absolute position of the element being dragged, based on the coordinates of the mouse? Or is there a prepackaged solution to this problems somewhere that I've missed?
All panels but Canvas use some kind of constraint to position their children. Only Canvas lets you use absolute positioning. That's why I think it is the only way to implement drag and drop .
Feel free to use a Canvas on top of your existing panel. Just remember to remove the dragged element from its original parent and put it in the Canvas (or drag some kind of copy) and do the reverse on mouse up.
One way of achieving absolutely positioning an item inside any container, not only Canvas, is to use transformation instead of Left/Top properties. For instance, to set at Left=50 Top=80 you can modify the margin values through transformation.
I'm trying to modify the default graph viewer of the Graph# library because its user interface is awful (just try dragging a node outside of the boundaries, you'll see!)
The basic setup is this: there is a GraphCanvas control (inherited from Panel) which has children of Vertex and Edge control types. What I want to achieve is:
GraphCanvas has scroll bars if the contents do not fit in the screen;
GraphCanvas can also be scrolled by "dragging" it (just click on an empty space and drag);
GraphCanvas can be zoomed in and out (via CTRL+mouse wheel);
Vertices can be dragged around. If a vertex is dragged outside the current boundaries of GraphCanvas, the boundaries are increased. The scroll bars should reflect this, however the current viewport should not scroll away while the vertex is being dragged . The same goes if dragging a vertex reduces the boundaries of GraphCanvas - it should stay the same size until the drag operation is finished and resize only then. Automatically scrolling the viewport during a drag operation is awfully confusing and easily introduces dragging errors. See the original implementation if you want to know what I mean.
Although I've got a fair bit of experience with .NET, I'm still a complete beginner in WPF. My current attempt is (in the measure/arrange layout phase) to give each vertext the XY coordinate it desires (even if negative) and implement zooming/scrolling by handling mouse events on the GraphCanvas and modifying the RenderTransform property. Dragging just changes the XY coordinates on the specific vertex (probably triggering the re-layout of the whole thing which would be nice to avoid too). Scrollbars are implemented by placing the GraphCanvas inside a ScrollViewer and implementing IScrollInfo on the GraphCanvas.
Unfortunately there seems to be a problem: I can get mouse events on the GraphCanvas itself only if it has a background at the point. That would be OK, I want a white background anyway, but in the negative coordinates of the GraphCanvas it does not draw the background - and thus does not respond to mouse events.
I'm also wondering if I'm doing the Right Thing by allowing all my child controls (vertices and edges) to go into negative coordinates. How would you implement this?
Added: To clarify about the background problem check out the following screenshot:
(source: valts.21.lv)
What you see here is a simple Windows Forms form with a WPF Host control on it. That has a ScrollViewer in it, and the ScrollViewer has the GraphCanvas in it. The GraphCanvas contains 4 vertices and 6 edges.
The GraphCanvas is stretched to fill the ScrollViewer. But since some of the vertices are at negative coordinates, it has a RenderTransform applied which simply shifts everything to the right (TranslateTransform). It also has a white background brush.
Note the gray area on the left. That's still a part of the GraphCanvas, but the background brush somehow doesn't exted there. Also, if I left-click there with my mouse (not on a node, but on the gray area), I do NOT get an event. If I left-click on the white area, I get all events just fine.
Call CaptureMouse on canvas.mouseDown and ReleaseMouseCapture on mouse up. Also, if you set your canvas background to transparent it will still be hit testable
You can attach a 'Draggable' behavior to each element.