有没有一种方法可以在WPF的XAML文件中绑定到鼠标位置?还是必须在代码中完成?我有一个放置在Canvas内的控件,当鼠标光标在Canvas内时,我只想让控件跟随鼠标移动。
谢谢
编辑:
好的,我找到了一个相对简单的方法,使用代码后台文件。我在Canvas上添加了MouseMove事件处理程序,然后添加了:
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
// Get the x and y coordinates of the mouse pointer.
System.Windows.Point position = e.GetPosition(this);
double pX = position.X;
double pY = position.Y;
// Sets the position of the image to the mouse coordinates.
myMouseImage.SetValue(Canvas.LeftProperty, pX);
myMouseImage.SetValue(Canvas.TopProperty, pY);
}
使用http://msdn.microsoft.com/zh-cn/library/ms746626.aspx作为指导方针。