繁体   English   中英

WPF - 如何将控件的 position 绑定到当前鼠标 position?

[英]WPF - how do I bind a control's position to the current mouse position?

有没有办法绑定到XAML文件中WPF中的鼠标position? 还是必须在代码中完成? 我在 Canvas 中有一个控件,我只想让控件跟随鼠标,而鼠标 cursor 在 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/en-us/library/ms746626.aspx作为指导。

为此,我尝试制作一种装饰器。 您将 object、鼠标 position 包装在您想要控制的上方并将一些控件绑定到装饰器 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>

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

“如何:制作 Object 跟随鼠标指针”应该是

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM