I'm creating a game that first requires the user to drag a certain textblock into a rectangle. Depending on which textblock is dropped into the rectangle, I want to store the contents of that specific textblock. It's important to note: I want the values stored only when the textblock is dropped inside the rectangle.
The problem: How can I make it so the computer knows that the rectangle contains the textblock?
Here's a screenshot to make things a little more clear: http://gyazo.com/3aa3a8678f11260889261fcd46366616.png
As of now, I can drag and drop the textblock fine, but the computer has no way of knowing if the textblock and rectangle intersect.
I've been trying to solve this problem for a few days now, and have spent a lot of time trying to use the system.drawing.rectangle.IntersectsWith()...only to find out that you can't dynamically add a system.drawing.rectangle to the canvas.
I've also thought of doing it by coordinates: if(textblock's coordinates are within the bounds of the rectangles coordinates)... However, I've spent time trying to figure out how to dynamically get the coordinate position of a xaml control, only to find out that you can't.
Could someone please offer some guidance? I've been working hard towards this and I've hit a dead end, so any degree of help will be great.
Thanks!
Related
I have seen lot of posts which demonstrate how to move objects in a Canvas but what I need is, a way to move either
1. the entire canvas along with its every child
or
2. move every object manually (which is certainly not advisable)
I have put my Canvas in a ScrollViewer.
My actual issue is : I m trying to zoom the canvas using ScaleTransform but after zooming, i also need to move the scroll viewer to a point such that the clicked point is at the center after zooming.
I tried Canvas.SetLeft() and Canvas.SetTop() but bad luck..
Any idea?
Thanks in Advance..
You can communicate with the ScrollViewer to make it scroll its content to a particular offset by using ScrollToVerticalOffset and ScrollToHorizontalOffset.
You'll need to calculate the correct offset by taking into account the size of the "viewport" i.e. the area that you can see of the content, and the zoom level.
http://go4answers.webhost4life.com/Example/center-zoom-wpf-problem-159135.aspx
This might be useful for what you are doing:
http://autoscroller.codeplex.com/
summarizing I have implemented a chart control as a simple Canvas with a Polyline on it. The next thing I need is to be able to zoom the chart.
I would like to know how would you that (just the idea, no details needed). What I would like to do is to create somehow a bigger Canvas and paint the line bigger and just show a part of the Canvas to the user, and the he drags the Chart it will move the Canvas. Something like in the following picture. Do you think this is possible?
Kael Rowan from Microsoft Research built a ZoomableCanvas class that may do exactly what you want. You can also see all the posts he wrote about it. You can even try a running XBAP example if your browser supports it.
We use the RenderTransform for this, create your zoom and pan matrix( or transform ) and apply that to your canvas. The nice thing is, that you can still have elements that can display behind or on top of the canvas with the identity transform or with another. For example for a grid or screen space elements like a minimap, which should always be visible. You might also want to look into this old question, which is somehow related.
I have a Canvas based custom control that enables selection, dragging and resizing of visual children. I have so far Lines, Rectangles (without background) and images. Problem is, for selection, lines for example are hard to select obviously as they are 1 pixel high. Could you suggest a way to make them easily selectable? I'd like something along the lines of Visual Studio's selection, in which there are some pixels of "miss tolerance" when selecting something thin like a grid, you don't need to hit the exact pixel the line is at, few pixels around it are just as good, but I'm not sure how to implement that in WPF. Please help?
Thank you.
Perhaps what you are looking for can be easily achievable by using Adorner
I am currently working on a Silverlight application using the Bing Map control, i am using a custom pushpin made up of an Image and a text block within a grid element, which works fine, this also allows me to have hover and right click events for each pushpin so all good so far, however there is a need for me to enable the user to drag the pushpin in any direction and have an a line connecting it to its actual position, sort of like an anchoring point.
I am struggling to see how to do this, I have seen lots of information on making the pushpins have the capability to be moved by the mouse but the line and anchor part is causing me a bit of a problem. Currently i keep my pushpins in various Observable Collections so that when they are updated the UI updates. If anyone has an Idea on a clean way of adding this functionality please let me know it will be much appreciated.
Many Thanks
I haven't got time to knock up a complete example. However I'll give away the basis of the solution. Replace the template with one based on a Canvas. For example you could have an ellipse to be the actual point on the map, a draggable border that has the ContentPresenter and a line to act as a tether.
The code needed is a) allow the Border to be dragged about and b) to move one end of the line about so that it lies under the Border.
I am still interested in other approaches but I have come up with a solution of paring my pushpin element with a MapPolyline, this in conjunction with the mouse handling to allow the user to drag the pushpin updates both, it is pretty effective and will do for now. I would still like to hear from anyone else with better suggestions though.
my current free-time project, in order to dive into WPF MVVM, is a "digital" copy of an old puzzle I used to play a lot in my childhood. It basically is a simple puzzle where one has to fill a given space with different kind of pieces so the whole space is filled. But with the extra twist of being in hexagonal space.
Just to illustrate, this is what it currently looks like in WPF:
http://img190.imageshack.us/img190/2553/atomgridmolecule.png
So basically there is a number of predefined pieces(like the orange one above) which can be "plugged" into the given grid(the gray stuff above).
So the result might look something like this:
http://img30.imageshack.us/img30/2553/atomgridmolecule.png
I want the user(probably only me^^) to be able to drag and drop the pieces into the grid. I want the dragging to look natural meaning having the correct offset while dragging depending on where the user clicked the piece.
Both grid and molecule are the same control, a custom hexagonal panel control derived from the WPF Panel class.
The problem is on how to do the "plugging in" and especially the "unplugging".
I have two ideas on how I might tackle this:
Just color the cells in the grid and hiding the original piece
Pro:
Zero cost perfect alignment of the cells
Cons:
Recreating the piece at the right spot with the correct mouse offset if dragging out, seems impossibly? hard to do
Snapping the piece to the grid and show it on top
Pro:
Dragging out is a simple dragging operation, just as dragging in
Disadvantage:
Somehow have to align the piece with the underlying grid, some kind of snapping
So which approach should I take? Even more important how can I even implement this in WPF? Especially using a clean MVVM way.
Thanks so much for your help! Any input is highly appreciated!
EDIT:
Thanks Aran, I thought so too.
But how do I actually implement this now?
How can I actually get the coordinates?
All the orange circles are linked, so how can I "move" or better "plug" them in as one piece?
Im inclined to go with the second idea. a simple snapping would just be to test if the centre point of the circle you are dragging is within some tolerance factor of a circle on the grid and if so snap them.