locked
how to set coordinates for an object during runtime? RRS feed

  • Question

  • Hi. I am trying to create an app that enables this: Users can create an instance of a given UserControl anywhere on the screen by clicking wherever they want it to appear. I can't figure out how to set position properties for the instance, though. Here is what I have so far:

    • On the MainWindow, via the Events panel, I have specified that any MouseDown event should call a "ClickPosition" method.
    • The "ClickPosition" method is listed below. There are two comments in it alongside the lines of code where I should be setting the coordinates of the UserControl instance. What should those properties be?

    Thanks in advance for any help.

    (p.s.: Can anyone suggest a good online reference for issues like this? I have spent all day looking through material scattered across the web, and it would be nice to have a single, comprehensive source -- one that's easier to look through than the SDK user guide, anyway.)

    ------------------------------------------------

    namespace myApp

    {

    public partial class MainWindow : Window

    {

    public MainWindow()

    { this.InitializeComponent(); }

    private void ClickPosition(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    Point mousePosition = e.GetPosition(null);

    double mousePositionX = e.GetPosition(null).X;

    double mousePositionY = e.GetPosition(null).Y;

    myUserControl uc = new myUserControl();

    uc.Width = uc.Height = 50;

    uc.? = mousePositionX;   //What goes here...?

    uc.? = mousePositionY;   //What goes here...?

    myGrid.Children.Add(uc);

    }
    }
    }

    ------------------------------------------------

    Monday, August 6, 2012 12:50 PM

All replies

  • Hello Ragamoffyn.

    What I believe you are missing is setting the margin of the UserControl.

    private void ClickPosition(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    	Point mousePosition = e.GetPosition(null);
    	
    	//You don't really need to declare the following doubles
    	//unless you intend to do some math with them.
    	double mousePositionX = mousePosition.X;
    	double mousePositionY = mousePosition.Y;
    	
    	myUserControl uc = new myUserControl();
    	uc.Width = uc.Height = 50;
    	
    	//If you don't set Horizontal and Vertical Alighnment to Top and Left
    	//it will default to the center.  It would consider the point 0,0 to be
    	//the center of the window instead of the top-left.
    	uc.HorizontalAlignment = HorizontalAlignment.Left;
    	uc.VerticalAlignment = VerticalAlignment.Top;
    	
    	uc.Margin = new Thickness(mousePosition.X, mousePosition.Y, 0, 0);  //Thickness(left, top, right, bottom)
    	
    	myGrid.Children.Add(uc);		
    }

    I personally use the MSDN library to search for answers.  You do have to be specific to your application for your search like adding C# and WPF or Silverlight to your search string.  But starting there could lead you to some sites you feel comfortable using.

    ~Christine


    My Gallery


    Monday, August 6, 2012 1:22 PM
  • If you are using Grid based layout Christine's answer should work. If you are using Canvas based layout (which only uses Top and Left coordinates) you could use:

    Canvas.SetTop(uc, mousePosition.X);

    Canvas.SetLeft(uc, mousePosition.Y);

    Monday, August 6, 2012 2:24 PM
    Moderator
  • Great! Thank you much, Christine and Chuck.
    Monday, August 6, 2012 8:15 PM