This is a mirror of official site: http://jasper-net.blogspot.com/

WPF: how to drag & drop a file in your window

| Monday, August 2, 2010
Have you ever wondered how the live messenger lets you drag and drop a file in a conversation window and send it right away ? …………………………………………………………………well, i did !  So i searched the web, found the solution and want to give it to you right away !

Lets say that we have a window that is filled only with a textbox and when we drop a .txt file on it, it shows the text of the file.

Open visual studio, create a new WPF project and add the following control in the main window:

<TextBox Name="FileShowTextBox" AcceptsReturn="True" />

Now the window should look like this:

<Window x:Class="WpfApplication2.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow" Height="350" Width="525">
   <Grid>
       <TextBox Name="FileShowTextBox" AcceptsReturn="True" />
   </Grid>
</Window>

In order to let the textbox accept a dropped file the AllowDrop property must be set to True, the PreviewDragEnter and PreviewDragOver events must be handled so that it is ensured that the correct file type is dropped and the PreviewDrop event that included the actions to be taken when the file is dropped. Now the textbox should look like this:

<TextBox Name="FileShowTextBox" AcceptsReturn="True" AllowDrop="True"
                PreviewDragEnter="FileShowTextBox_PreviewDragEnter"
                PreviewDragOver="FileShowTextBox_PreviewDragEnter"
                PreviewDrop="FileShowTextBox_PreviewDrop" />


The FileShowTextBox_PreviewDragEnter event handler checks the dropping object’s format and if it is a .txt file, it accepts it. Add the following code in the event handler:

     private void FileShowTextBox_PreviewDragEnter(object sender, DragEventArgs e)
     {
           bool isCorrect = true;

           if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
           {
               string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
               foreach (string filename in filenames)
               {
                   if (File.Exists(filename) == false)
                   {
                       isCorrect = false;
                       break;
                   }
                   FileInfo info = new FileInfo(filename);
                   if (info.Extension != ".txt")
                   {
                       isCorrect = false;
                       break;
                   }
               }
           }
           if (isCorrect == true)
               e.Effects = DragDropEffects.All;
           else
               e.Effects = DragDropEffects.None;
           e.Handled = true;
       }

The final step is to add the actions to be taken when the text file is dropped in the event handler of the Drop event.Add the following code in the FileShowTextBox_PreviewDrop function:

       private void FileShowTextBox_PreviewDrop(object sender, DragEventArgs e)
       {
           string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
           foreach (string filename in filenames)
               FileShowTextBox.Text += File.ReadAllText(filename);
           e.Handled = true;
       }


Read more: Development Romance

Posted via email from .NET Info

0 comments: