I need to get element from canvas by given point.
For example I have Rectangle on Canvas, which CanvasLeft and CanvasTop values are setted to some values.
I whant get element from canvas which CanvasLeft and CanvasTop vaules are for example 10 and 40.
Is it possible?
Thanks.
You can find the elements a specific position by using the VisualTreeHelper. It has a method FindElementsInHostCoordinates. You'll have to give it a host, for example the canvas and the coordinates and it returns a list of UIElements.
Here's the info on MSDN:
http://msdn.microsoft.com/en-us/library/cc838402(v=VS.95).aspx
Code like this should do it:-
UIElement elem = VisualTreeHelper.FindElementsInHostCoordinates(new Point(10, 40), myCanvas).FirstOrDefault();
Related
I have the property RelativePosition in class MapItem is the relative position of the working point within the cell that contains it. The two components of the vector are always in the range [0,1]. In our example image below the corrdinates would be something like (0.25, 0.05).
Each item has a property RelativePosition which is a Vector that defines the working point position relative to the item. For example (0,0) is the top-left corner and (1,1) is the bottom-right.
How to draw a rectangle in a cell throught relative position ? Thanks for help me?
Have you thought about positioning the image by using containers? Maybe UniformGrid could fit for you. If you want the middle of the image at 0.25 from the left center it in the left cell of an UniformGrid with two columns.
I'm trying to figure out the algorithm for scaling multiple selected objects on a canvas (similar to Visio's behavior). Say for instance I have the following selected objects in my application:
I then drag the lower-right handle of the bounding box to increase the size of the selected objects and thus produce the following results:
My questions are as follows:
How do I get the amount of scaling to be applied to each object?
How do I get the amount of translation to be applied to each object?
I hope this question makes sense. And I hope you could help.
Hi I dont think there is any Translation , There is only Scaleing . One easy way to do that is preserve the Width and Height of your object like (TextBoxes above) and then when you want to get Scaleing values of that object
ScaleTransform scale = new ScaleTransform();
//_text is the scaled object
scale.ScaleX = text.ActualWidth - _width; //_width is width of the textbox at beginning.
scale.ScaleY = text.ActualHeight - _height; //_height is the height of textbox at the beginning.
This will give you the amount by which object is scaled corressponding to the Width and Height of TextBox at the beginning (i.e When window initialized) . I hyope this will give you an idea.
My problem is I have paths to draw cities and I want to put images inside these cities I cant put Image inside <Path> tags.
Why cant I write such code? any solution?
You can use the Path.Fill for this. Use an ImageBrush.
On a side-note, what can be written as child node/s of a element is determined by the ContentPropertyAttribute, for a ContentControl it points to the Content property for example (as can be seen in the Syntax section of its documentation).
Use a canvas or a custom panel with x,y placements and add images to the correct coordinates (same coordinates used by path).
You can query the path nodes to find their coordinates if you don't know them.
Edit: Since your window size changes and coordinates "stretch" accordingly, you will need to implement a custom panel. - Inherit from Panel and override measure and arrange methods as in this example: http://www.wpftutorial.net/CustomLayoutPanel.html
Your custom panel should have X and Y attached dependency properties (like Canvas does) only that methods should use either relative coordinates (0 to 1) instead of (0 to width and 0 to height) or divide by original path width and height to normalize to coordinates.
I have a problem with my adorner which is supposed to preview the destination of my drag & drop operation,
everything is working fine until i rotate the adorned element.
My adorner is a rectangle which is filled with a visual brush of the adorned element, so if the element is rotated the adorner gets the correct (already rotated) image. But because the rectangle has to be rotated too the image gets rotated once more which isn't supposed to happen. Can I somehow exclude the brush of the transformation so it will not rotate again or is there another solution for my problem?
Thanks
You can override the GetDesiredTransform method in the adorner class and manipulate the transforms applied to the adorner's children (the rectangle with the visual brush in your case).
Finally I have a solution to this problem:
with this small algorythm link I've created a copy of the UI element, set its rendertransform property to null and used it as a visualbrush instead of the original element which also allowed me to set the original element to invisible!
In Silverlight, I'm trying to draw a line on a Canvas by setting the LineGeometry element's EndPoint to the cursor position. In the Canvas' MouseMove event, I'm doing this:
line.EndPoint = e.GetPosition(this);
where "this" is the Canvas.
"line" is a LineGeometry element added to a GeometryGroup, and the GeometryGroup is added to a path object, which is added to the Canvas' Children collection.
The line does not show up on the canvas. What causes this?
This isn't an area that I understand very well, and I'm only answering because nobody else has answered yet :-). But I think the issue might be that the line's EndPoint is defined relative to its container (the GeometryGroup), but the Point structure returned by e.GetPosition(this) is relative to the containing control. You may need to apply a Transform to the e.GetPosition(this) to get the correct point. See here for more details on how transforms work.