Click here to Skip to main content
16,023,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Say I put a Shape on a Canvas like this:
Canvas.SetLeft(myShape, 50);
Canvas.SetTop(myShape, 30);
canvas.Children.Add(myShape);

After that, myShape was drag & dropped on this Canvas.
How can I determine the current myShape coordinates ?
For example, before the drag & drop I would like to get (50,30).
Thanks !

Thanks for your answers !
1. Could you please show me an example of setting the position of the Shape by setting it's margins ?
2. What is the most used method to specify the Shape coordinates on a Canvas ? I would expect something like: canvas.Children.Add(myShape, 50, 30), but I don't see such method.
Posted
Updated 8-Jan-10 23:06pm
v3

I would set the position by setting the top and left margin of the shape, then I would check those properties.
 
Share this answer
 
Canvas.GetLeft and Canvas.GetTop.
 
Share this answer
 
Ooops - small brain fart there.
 
Share this answer
 
v2
Not sure what Christian is talking about. This seems to work:
XML
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Canvas Name="C1">
            <Button Name="B1" Canvas.Left="200" Canvas.Top="100" Content="1" />
            <Button Name="B2" Canvas.Left="20" Canvas.Top="10" Content="2" />
        </Canvas>
    </Grid>
</Window>

C#
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
			string str = Canvas.GetLeft(B1) + "," +
				Canvas.GetTop(B1) + "_" + Canvas.GetLeft(B2) +
				"," + Canvas.GetTop(B2);
			MessageBox.Show(str);
		}
	}
}
 
Share this answer
 
Please ask new questions as new questions, rather than modifying your original question. You add the shape to the canvas children, then you use Canvas.SetLeft and Canvas.SetTop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900