Draw line in canvas
-
Sunday, April 15, 2012 5:45 AM
I have two questions with this.
My form as a grid and in one column and row of my grid i have my canvas.
How do I find the X and Y position of this canvas?
How can I draw a line within this canvas?
All Replies
-
Sunday, April 15, 2012 10:31 AM
Hi kiwis,
There are properties available like Canvas.Top, Canvas.Left, etc, which will give you position of canvas.
And about lines, you can draw inside canvas as:
<Canvas Height="300" Width="300"> <!-- Draws a diagonal line from (10,10) to (50,50). --> <Line X1="10" Y1="10" X2="50" Y2="50" Stroke="Black" StrokeThickness="4" /> </Canvas>
Regards, http://shwetamannjain.blogspot.com
- Proposed As Answer by Shweta Jain Sunday, April 15, 2012 10:31 AM
-
Sunday, April 15, 2012 11:27 AM
I suppose you are trying to draw the line from code, so i would first and foremost find the object in the Grid that contains the canvas, and then
line = new Line(); line.Stroke = Brushes.LightSteelBlue; line.X1 = 1; line.X2 = 50; line.Y1 = 1; line.Y2 = 50; line.StrokeThickness = 2; myCanvas.Children.Add(line);
Hope this helps.
Developing is part of being a developer.
- Marked As Answer by Annabella LuoModerator Thursday, April 26, 2012 5:56 AM
-
Monday, April 16, 2012 6:24 PMModerator
Hi Kiwis,
To find the position of Canvas, there is the TransformToAncestor method:
Point relativePoint = Canvas1.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));Where
Canvasis the element which you want to get position, androotVisualis Application.Current.MainWindow or whatever you want the position relative to.Hope it helps.
Have a nice day.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Annabella LuoModerator Thursday, April 26, 2012 5:56 AM

