Drag拖 Drop放 合起來DragDrop拖曳

1.AllowDrop 為該物件可以接受其他物件的Drop放

      myCanvas.AllowDrop = true;

2.設定動作

      myCanvas.Drop += new DragEventHandler(Canvas_Drop);

      //當按下滑鼠左鍵
      btn.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseDown);

3.將按鈕Button按下後拖曳到Canvas 增加一個Button

      private void btn_MouseDown(object sender, MouseButtonEventArgs e){
            Button btn = e.Source as Button;
            System.Windows.DataObject data = new System.Windows.DataObject(typeof(Button), btn);
            DragDrop.DoDragDrop(btn, data, System.Windows.DragDropEffects.Copy);          
        }
        private void Canvas_Drop(object sender, System.Windows.DragEventArgs e){
            Button btn = e.Data.GetData(typeof(Button)) as Button;
            Button btnCopy = new Button() { Width = 90, Height = 50, Content = btn.Content };         
            Canvas obj = (Canvas)sender;
            obj.Children.Clear();
            Canvas.SetLeft(btnCopy, e.GetPosition(obj).X);
            Canvas.SetBottom(btnCopy, e.GetPosition(obj).Y);
            obj.Children.Add(btnCopy);
        }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 newworldmaking 的頭像
    newworldmaking

    新世界making

    newworldmaking 發表在 痞客邦 留言(0) 人氣()