WPF鼠標拖放功能(拖放圖片,文本)


      對於拖放操作有兩個方面:源和目標。為了創建拖放源,需要在某個位置調用DragDrop.DoDragDrop()方法初始化拖放操作。此時確定拖動操作的源,擱置希望移動的內容,並指明充許什么樣的拖放效果(復制,移動等)。

      通常會在響應PreviewMouseDown或MouseDown事件時,調用DoDragDrop()方法。

      而接收的元素需要將它的AllowDrop屬性設置為true,還需要通過處理Drop事件來處理數據。

 前台代碼:

     < Grid >
         < Grid.RowDefinitions >
             < RowDefinition  Height ="*" ></ RowDefinition >
             < RowDefinition  Height ="*" ></ RowDefinition >
         </ Grid.RowDefinitions >
         < Grid.ColumnDefinitions >
             < ColumnDefinition  Width ="*" ></ ColumnDefinition >
             < ColumnDefinition  Width ="*" ></ ColumnDefinition >
         </ Grid.ColumnDefinitions >
         < TextBox  Name ="source"  Grid.Row ="0"  Background ="Purple"  Foreground ="White"  MouseDown ="source_MouseDown" >博客園:www.cnblogs.com </ TextBox >
         < Image  Source ="http://static.cnblogs.com/images/logo_small.gif"  Grid.Column ="1"  Stretch ="None"  MouseDown ="Image_MouseDown" ></ Image >
         < Label  Name ="target"  Grid.Row ="1"  Background ="YellowGreen"  AllowDrop ="True"  Drop ="OnDrop" >文本拖到這里 </ Label >
         < Image  Name ="targetImg"  Grid.Row ="1"  Grid.Column ="1"  AllowDrop ="True"  Drop ="targetImg_Drop_1"  Stretch ="None"  Source ="http://www.baidu.com/img/baidu_sylogo1.gif" ></ Image >
     </ Grid >

 

后台代碼:

        ///   <summary>
        
///  文本源數據
        
///   </summary>
         private  void source_MouseDown( object sender, MouseButtonEventArgs e)
        {
            TextBox objText = sender  as TextBox;
            DragDrop.DoDragDrop(objText, objText, DragDropEffects.Copy);
        }

         ///   <summary>
        
///  圖片源數據
        
///   </summary>
         private  void Image_MouseDown( object sender, MouseButtonEventArgs e)
        {
            Image objImage = sender  as Image;
            DragDrop.DoDragDrop(objImage, objImage.Source, DragDropEffects.Copy);
        }


         ///   <summary>
        
///  目標位置
        
///   </summary>
         private  void OnDrop( object sender, DragEventArgs e)
        {
             if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effects = DragDropEffects.Copy;
             else
                 return;

             this.target.Content = e.Data.GetData(DataFormats.Text);
        }


         private  void targetImg_Drop_1( object sender, DragEventArgs e)
        {
            e.Data.GetFormats();
             if (e.Data.GetDataPresent( " System.Windows.Media.Imaging.BitmapFrameDecode "))
                e.Effects = DragDropEffects.Copy;
             else
            {
                 return;
            }
             //  targetImg.Source = (ImageSource)e.Data.GetData("System.Windows.Media.Imaging.BitmapFrameDecode");
            ((Image)sender).Source = (ImageSource)e.Data.GetData( " System.Windows.Media.Imaging.BitmapFrameDecode ");
        }

 

     如果不知道拖放源數據是什么數據類型,可以使用實現了IDataObject接口的GetFormats()方法。如:   e.Data.GetFormats();  其中Data就實現了IDataObject接口。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM