簡體   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