WPF Control不会在单击和拖动时跟踪鼠标移动

时间:2016-05-11 20:18:32

标签: c# wpf xaml

很抱歉,如果这是一个糟糕的问题,但是我试图在控件上获得点击和拖动效果,只要按下鼠标左键,控件就会跟随用户鼠标移动。我首先按照本教程获得基本的点击和拖动功能https://msdn.microsoft.com/en-us/library/hh144799(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-7

我稍微修改了一下代码,试着让控件跟进。

Circle.xaml

true

Circle.xaml.cs

WITH RECURSIVE category_tree(id, name, depth) AS (
  SELECT id, name, ARRAY[id]
  FROM category
  WHERE parentid IS NULL
UNION ALL
  SELECT category.id, category.name, depth || category.id
  FROM category_tree
  JOIN category ON category.parentid=category_tree.id
  WHERE NOT category.id = ANY(depth)
)
SELECT * FROM category_tree ORDER BY id;

MainWindow.xaml

<UserControl x:Class="DragDropExample.Circle"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DragDropExample"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         AllowDrop="True">
    <UserControl.Resources>
        <Storyboard x:Key="TestStoryboard">
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)" To="0"/>
            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1"/>
        </Storyboard>
    </UserControl.Resources>
    <Canvas x:Name="canv">
        <Ellipse x:Name="circleUI" Height="100" Width="100" Fill="Blue"/>
    </Canvas>
</UserControl>

MainWindow.xaml.cs

namespace DragDropExample
{
public partial class Circle : UserControl
{
    private Brush _previousFill = null;
    private bool _isDragging;

    public Circle()
    {
        InitializeComponent();
    }

    public Circle(Circle c)
    {
        InitializeComponent();
        this.circleUI.Height = c.circleUI.Height;
        this.circleUI.Width = c.circleUI.Height;
        this.circleUI.Fill = c.circleUI.Fill;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DataObject data = new DataObject();
            data.SetData(DataFormats.StringFormat, circleUI.Fill.ToString());
            data.SetData("Double", circleUI.Height);
            data.SetData("Object", this);

            Point position = e.GetPosition(this);
            double x = position.X;
            double y = position.Y;

            Canvas.SetLeft(circleUI, x);
            Canvas.SetTop(circleUI, y);

            DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);
        }
    }

    protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
    {
        base.OnGiveFeedback(e);

        if (e.Effects.HasFlag(DragDropEffects.Copy))
        {
            Mouse.SetCursor(Cursors.Cross);
        }
        else if (e.Effects.HasFlag(DragDropEffects.Move))
        {
            Mouse.SetCursor(Cursors.Pen);
        }
        else
        {
            Mouse.SetCursor(Cursors.No);
        }

        e.Handled = true;
    }

    protected override void OnDrop(DragEventArgs e)
    {
        base.OnDrop(e);

        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {
            string dataString = (string) e.Data.GetData(DataFormats.StringFormat);

            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                Brush newFill = (Brush) converter.ConvertFromString(dataString);
                circleUI.Fill = newFill;

                if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.Move;
                }
            }
        }
        e.Handled = true;
    }

    protected override void OnDragOver(DragEventArgs e)
    {
        base.OnDragOver(e);
        e.Effects = DragDropEffects.None;

        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {
            string dataString = (string) e.Data.GetData(DataFormats.StringFormat);

            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.Move;
                }
            }
        }
        e.Handled = true;
    }

    protected override void OnDragEnter(DragEventArgs e)
    {
        base.OnDragEnter(e);
        _previousFill = circleUI.Fill;

        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {
            string dataString = (string) e.Data.GetData(DataFormats.StringFormat);

            BrushConverter converter = new BrushConverter();
            if (converter.IsValid(dataString))
            {
                Brush newFill = (Brush) converter.ConvertFromString(dataString);
                circleUI.Fill = newFill;
            }
        }
    }

    protected override void OnDragLeave(DragEventArgs e)
    {
        base.OnDragLeave(e);
        circleUI.Fill = _previousFill;
    }
}
}

这是非常简单的代码,当我点击圆圈并按下鼠标左键移动鼠标时,它会向右和向下跳过屏幕。我想知道我是否应该在圆圈上使用故事板或某种转换来让它移动。我确定它只是一些我想念的简单事物,但它现在正在逃避我。我非常感谢任何建议。

0 个答案:

没有答案