WPF - 如何将控件位置绑定到当前鼠标位置?

9
有没有一种方法可以在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作为指导方针。

2个回答

11

我尝试着为此创建一种装饰器。您只需要包装要控制鼠标位置,并将某些控件绑定到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>

谢谢,这对我非常有用,不仅可以获取鼠标位置,还可以使用装饰器“提取”其他属性。 - pkr

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接