有没有办法在 XAML 文件中绑定到 WPF 中的鼠标位置?还是必须在代码中完成?我在画布内有一个控件,我只想让控件在鼠标光标位于画布内时跟随鼠标。
谢谢
编辑:
好的,我想出了一个使用代码隐藏文件的相对简单的方法。我在 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/en-us/library/ms746626.aspx作为指导。