Windows窗体拖放功能

时间:2017-03-29 16:05:34

标签: c# winforms visual-studio-2015 drag-and-drop

我似乎拥有Drag-n-Drop所需的所有功能,但是当我尝试将某个控件从一个控件拖到另一个控件时,我的代码不会被调用。

  • 我创建了一个用于创建数据的测试类:

    class TestClass
    {
    
        public TestClass()
        {
            Name = "Blank";
            Id = -1;
        }
    
        public String Name { get; set; }
    
        public int Id { get; set; }
    
        public object Data { get; set; }
    
        public override String ToString()
        {
            return String.Format("{0}: {1}", Id, Name);
        }
    
    }
    
  • 我创建了一个包含2个列表视图控件的空表单listView1listView2

ListViewsForm

  • 我为所有Drag-n-Drop方法创建了事件处理程序,并在它们上添加了断点:

    private void Form_DragDrop(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
        if (e.Data.GetDataPresent(typeof(TestClass)))
        {
            var item = e.Data.GetData(typeof(TestClass)) as TestClass;
            if (item != null)
            {
                // ?
            }
        }
    }
    
    private void Form_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
        if (sender.Equals(listView2))
        {
            e.Effect = DragDropEffects.All;
            if (e.Data != null)
            {
                var format = GetString(e);
                Console.WriteLine("{0} has a(n) {1} entering it.", listView2.Name, format);
            }
        }
    }
    
    private void Form_DragLeave(object sender, EventArgs e)
    {
        if (sender.Equals(listView2))
        {
    
        }
    }
    
    private void Form_DragOver(object sender, DragEventArgs e)
    {
        // Code Project Article 9017 says DragOver fires repeatedly, even if the mouse does not move.
        if ((e.X != _lastX) || (e.Y != _lastY))
        {
            _lastX = e.X;
            _lastY = e.Y;
            if (e.Data.GetDataPresent(typeof(TestClass)))
            {
    
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
    
    private void Form_GiveFeedback(Object sender, GiveFeedbackEventArgs e)
    {
        if (sender.Equals(listView2))
        {
            // ?
        }
    }
    
    private void Form_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
    {
        var listView = sender as ListView;
        if (listView != null)
        {
    
        }
    }
    
    private String GetString(DragEventArgs e)
    {
        var result = String.Empty;
        if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
        {
            var formats = e.Data.GetFormats();
            foreach (var format in formats)
            {
                Console.WriteLine("Data is in the format of [{0}].", format);
                result = format;
            }
        }
        return result;
    }
    
  • 我在构造函数InitializeComponent();之后连接了两个控件:

    public ListViewsForm()
    {
        InitializeComponent();
        // Setup ListView 1:
        listView1.AllowDrop = true;
        listView1.DragDrop += Form_DragDrop;
        listView1.DragEnter += Form_DragEnter;
        listView1.DragLeave += Form_DragLeave;
        listView1.DragOver += Form_DragOver;
        listView1.GiveFeedback += Form_GiveFeedback;
        listView1.QueryContinueDrag += Form_QueryContinueDrag;
        listView1.Dock = DockStyle.Fill;
        listView1.View = View.List;
        // Setup ListView 2:
        listView2.AllowDrop = true;
        listView2.DragDrop += Form_DragDrop;
        listView2.DragEnter += Form_DragEnter;
        listView2.DragLeave += Form_DragLeave;
        listView2.DragOver += Form_DragOver;
        listView2.GiveFeedback += Form_GiveFeedback;
        listView2.QueryContinueDrag += Form_QueryContinueDrag;
        listView2.Dock = DockStyle.Fill;
        // Create some data:
        var group1 = new ListViewGroup("Known Colors");
        foreach (KnownColor known in Enum.GetValues(typeof(KnownColor)))
        {
            var item = new TestClass()
            {
                Name = known.ToString(),
                Id = (int)known,
                Data = known,
            };
            var color = Color.FromKnownColor(known);
            var lvi = new ListViewItem(item.ToString(), group1) {
                BackColor = Color.FromArgb(color.ToArgb() ^ 0xffffff),
                ForeColor = color,
                Tag = item,
            };
            listView1.Items.Add(lvi);
        }
    }
    

每当我运行项目时,一切都很好,但是任何将项目从LEFT侧拖到RIGHT侧的尝试都不起作用。没有一个破发点被击中。没有任何事情发生。

我遗漏了什么?

1 个答案:

答案 0 :(得分:1)

您缺少一个方便的事件和开始拖动操作的方法:

listView1.ItemDrag += listView1_ItemDrag;

void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
  DoDragDrop(e.Item, DragDropEffects.Copy);
}