WPF实现拖拽功能

两个content控件之间实现拖拽功能,在一个控件中启动拖拽操作,在另一个控件中处理事件,代码如下,在两个listview之间拖拽:

//在启动拖拽控件的MouseMove事件里启动拖拽
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
     if (e.LeftButton == MouseButtonState.Pressed)
     {
           ListView lv = sender as ListView;
           MyClass selectedItem = lv.SelectedItem as MyClass;
           if (selectedItem == null)
           {
               return;
           }
           DataObject dataObject = new DataObject(typeof(MyClass), selectedItem);
           DragDrop.DoDragDrop(mylistView1, dataObject, DragDropEffects.Copy);
     }
}
//在另一个控件的Drop事件的实现拖拽
private void listView2_Drop(object sender, DragEventArgs e)
{
    var data = e.Data;
    if (data.GetDataPresent(typeof(MyClass)))
    {
        var myClass = data.GetData(typeof(MyClass)) as MyClass;
        GrouViewData.AddMulGoupItemFunction(myClass);
    }
}





你可能感兴趣的:(WPF,C#)