I have an area in my application that contains toolbar, my toolbar is consist of movable canvas, that I can drag and drop it anywhere, what I want to do is to restrict the user and not allow the toolbar to be moved out of this area,
and make it dockable so I can dock it to the left, right, top and bottom of this specified area,
can anyone help me in this case?
bellow snapshot show the toolbar is highlighted by red and the restricted area is highlighted by black this area the user can't move the toolbar out
Here is the snapshot
You can reset the toolbar's position after a drag operation is completed.
First get the toolbar's coordinate which is relative to the Canvas, then translate it to coordinate relative to the bound (black area).
You can then calculate how close the toolbar's center is from each edge of the bound. If the distance is lower than a defined value, move the toolbar to the edge (to dock it), you need to rotate the toolbar 90 degrees if you dock it to the left and right edges.
Something like this.
Related
A WPF application consists of a uniform grid of arbitrary size. Each cell in the grid contains a canvas. Each canvas is a target for a drag and drop operation.
When I drag another canvas and drop it onto a canvas inside the grid I want the top left corners of the dragged canvas(source) and the target canvas to align, essentially placing the source on top of the target. The behaviour I'm looking for is a snap-to-grid effect.
Currently when I use element.GetValue(Canvas.LeftProperty) the result in NaN. The problem is to determine the position of the corner for a canvas inside the grid, but relative to the entire window. I would prefer to use the grid, as it automatically resize as the window resize.
Is it possible to get the actual position of a canvas inside a grid even when the grid size changed or alternatively specify a canvas to be aligned to another canvas inside the grid?
This problem was solved by drawing each canvas onto another canvas by specifying the top and left properties of the canvas, I had to scratch the grid. I specified the position of every canvas by using
Canvas.SetLeft(Me, position.X)
Canvas.SetTop(Me, position.Y)
where Me is an instance of the canvas.
It is now easy to get the top and left properties of every canvas. The only problem now is I have to implement the resizing of every canvas manually.
I'm a newbie. Designing a form that can be resized, and I want my textboxes, labels and buttons to resize with the form, can someone tell me how to do this?
It depends on the type of layout you need. The "basic tools" you have to do that are following properties: Anchor and Dock.
Anchor
With the Anchor property you "attach" a side of an element to a side of its container. For example if you place a button in the bottom-right corner of a window and you set "Bottom, Right" as Anchor then when you'll resize the form the button will keep its relative position to that corner.
Now imagine you place a multiline text-box in the form, resize as needed (for example 4 px from top, left and right border and 128 px height) and set the Anchor property to "Left, Top, Right". When you'll resize the form that control will keep its height but it'll resize to keep its margins (so if you'll make the form wider its width will be increased).
Dock
Dock is different. With docking you "say" to the Layout Manager to use all available space in one direction. For example if you set to Left then your control will keep its width but it'll use all the available height and its location will be most left as possible.
You may have more than one control docked in a container, imagine you have 5 textbox with Top docking inside a form. They'll be stacked to the top of the form using all the width (and resizing). Another example: a Top docked control (as a banner) and a "Fill" docked control (as main content). Remember that with docking the order of controls matters (if you first place the "Fill" control it'll use ALL the available space and the "Top" dock control will overlap).
Even more
Moreover you have some layout controls too (tables and stacks). They're really easy to use and a 30 minutes of "experiments" will clarify much better than a long text.
The application will have a menu strip on top and the rest will display lines and shapes. I want to be able to drag and pan the main display area by clicking it and dragging it in any direction.
I am currently drawing items using pixel values (for example a line drawn from (1042,54)to(1240,104). But I think a monitor with a smaller resolution will not be able to display that object. That is why I want to be able to pan the display area.
The lines and shapes are currently being drawn on a gird that I placed inside the window.
If you place the canvas inside a ScrollViewer then if the canvas is larger than the application window it will be displayed with scroll bars.
You can then address points on the canvas with values relative to the canvas rather than the screen.
The main window of our WPF application consists of 4 regions, arranged as 2x2 tiles. The user must be able to resize these regions. This is not a problem when using a Grid control with GridSplitter elements.
But there is an additional requirement. In the center of window where all the regions meet there has to be a button which can be used to resize the regions too. The user can drag the button around. As a result all regions resize. E.g. if the user drags the button to the left the width of both regions on the left decreases. If the button is dragged to the bottom the height of the top regions increase while the height of the bottom regions decrease.
How can I do this using the WPF technology?
Use a Thumb as the central button, and handle its drag events (DragStarted, DragDelta, DragComplete) to move the GridSplitters at the correct position
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.