如何在运行时设置对象的坐标? [英] how to set coordinates for an object during runtime?

查看:52
本文介绍了如何在运行时设置对象的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在尝试创建一个启用此功能的应用程序:用户可以通过单击他们想要显示的位置在屏幕上的任何位置创建给定UserControl的实例。但是,我无法弄清楚如何为实例设置位置属性。到目前为止我只需要



  • 在MainWindow上,通过Events面板,我已经指定任何MouseDown事件都应该调用" ; ClickPosition"方法。
  • "ClickPosition"方法如下。在代码行中有两条注释,我应该设置UserControl实例的坐标。这些属性应该是什么?

提前感谢您的帮助。


(ps:任何人都可以建议一个很好的在线参考这样的问题?我花了一整天时间浏览分散在网络上的资料,如果有一个单一的,全面的资源 - 一个比SDK
用户指南更容易查看的资源,那将是很好的。)


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


命名空间myApp


{


公共部分类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;   //这里有什么......?


UC? = mousePositionY;   //这里有什么......?


myGrid.Children.Add(uc);


}

}

}


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

解决方案

Hello Ragamoffyn。


我认为你缺少的是设置UserControl的边距。

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

//你真的不需要声明以下双打
//除非你打算用它们做一些数学计算。
double mousePositionX = mousePosition.X;
double mousePositionY = mousePosition.Y;

myUserControl uc = new myUserControl();
uc.Width = uc.Height = 50;

//如果你没有将水平和垂直对齐设置为顶部和左边
//它将默认为中心。它会将点0,0视为
//窗口的中心而不是左上角。
uc.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left;
uc.VerticalAlignment = VerticalAlignment.Top;

uc.Margin = new Thickness(mousePosition.X,mousePosition.Y,0,0); //厚度(左,上,右,下)

myGrid.Children.Add(uc);
}

我个人使用
MSDN库
以搜索答案。 您必须特定于您的搜索应用程序,例如将C#和WPF或Silverlight添加到搜索字符串中。 但是从那里开始可能会带你到一些你觉得舒服的网站。




~Christine


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);

}
}
}

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

解决方案

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


这篇关于如何在运行时设置对象的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆