9

有没有办法在 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作为指导。

4

2 回答 2

10

为此,我尝试制作一种装饰器。您包装对象,您想要控制的鼠标位置并将一些控件绑定到装饰器 MousePosition 属性。

public class MouseTrackerDecorator : Decorator
{
    static readonly DependencyProperty MousePositionProperty;
    static MouseTrackerDecorator()
    {
        MousePositionProperty = DependencyProperty.Register("MousePosition", typeof(Point), typeof(MouseTrackerDecorator));
    }

    public override UIElement Child
    {
        get
        {
            return base.Child;
        }
        set
        {
            if (base.Child != null)
                base.Child.MouseMove -= _controlledObject_MouseMove;
            base.Child = value;
            base.Child.MouseMove += _controlledObject_MouseMove;
        }
    }

    public Point MousePosition
    {
        get
        {
            return (Point)GetValue(MouseTrackerDecorator.MousePositionProperty);
        }
        set
        {
            SetValue(MouseTrackerDecorator.MousePositionProperty, value);
        }
    }

    void _controlledObject_MouseMove(object sender, MouseEventArgs e)
    {
        Point p = e.GetPosition(base.Child);

        // Here you can add some validation logic
        MousePosition = p;            
    }
}

和 XAML

<local:MouseTrackerDecorator x:Name="mouseTracker">
    <Canvas Width="200" Height="200" Background="Red">
        <Button Width="20" Height="20" Canvas.Left="{Binding ElementName=mouseTracker, Path=MousePosition.X}" Canvas.Top="{Binding ElementName=mouseTracker, Path=MousePosition.Y}"  />
    </Canvas>
</local:MouseTrackerDecorator>
于 2011-07-16T16:52:03.430 回答
0

只试了几个例子。我认为 msdn 文档的措辞不正确

“如何:使对象跟随鼠标指针” 应该是

无论如何,“如何:根据鼠标位置增加对象的大小” 。

我能够通过更改画布属性来实现这种效果。也不确定为什么每个人都将事件处理程序附加到对象的下一个顶级布局属性而不是窗口。也许你和网上的大多数例子都会产生不同的效果

<Window x:Class="FollowMouse.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"  MouseMove="MouseMoveHandler">
<Canvas>
    <Ellipse Name="ellipse" Fill="LightBlue"Width="100" Height="100"/>
</Canvas>

后面的代码

private void MouseMoveHandler(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 eclipse to the mouse coordinates.
     Canvas.SetLeft(ellipse, pX);
     Canvas.SetTop(ellipse, pY);
     Canvas.SetRight(ellipse, pX);
  }
于 2012-11-22T07:57:51.963 回答