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; }
Now we are ready to test our making. Run the application, drag and drop a text file and see the results.
Cool post! DataFormats is a really helpful .NET class.
Thanks!
Pingback from Twitter Trackbacks for WPF: how to drag & drop a file in your window - Development Romance [studentguru.gr] on Topsy.com
Pingback from Easy Installation Of Mini Blinds « e377
In this post we saw the way to drop files in a WPF window, now we are going to see a way to drag and
This site is just the mine of information! Your simple and strict instructions helped to solve all the problems!
@essay company Thank you !!
Your site is amazing.I am very impressed to see this,i want to come back for visiting your site.Keep doing Good as well as you can..
Cheap NFL,NBA,MLB,NHL [url=http://www.annajerseys.com/]Wholesale Jerseys From China[/url].[url=http://www.annajerseys.com/]China Jerseys[/url],[url=http://www.annajerseys.com/]Sports Jerseys China[/url],[url=www.annajerseys.com/nfl-jerseys-c-1.html]NFL Jerseys China[/url],NBA Jerseys,NHL Jerseys,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[/pre]
[pre]
We Are Professional China Brand Wholesaler,[url=www.anna-wholesale.com/brand-shoes-c-380.html]Wholesale Brand Shoes[/url],Handbags,[url=www.anna-wholesale.com/sports-jerseys-c-379.html]Sports Jerseys China[/url],Jewelry,Hats,[url=www.anna-wholesale.com/sunglasses-c-382.html]Sunglasses From China[/url],Cheap [url=http://www.anna-wholesale.com/]China Wholesael[/url],Wholesale From China,Free Shipping,Cheap Price,7 Days Deliver
Best Converse Online Store From UK,Sale 2011 Newest [url=http://www.sale-converse.com/]Converse All Stars[/url],[url=www.sale-converse.com/converse-flag-shoes-c-130.html]Converse Flag Shoes[/url],Black Converse,White Converse,Navy Converse And So on,[url=http://www.sale-converse.com/]Converse UK[/url],Free Shipping
We are professional jerseys manufacturer from china,wholesal sports jerseys From China,[url=www.anna-jersey.com/nfl-jerseys-c-773.html]NFL jerseys China[/url],[url=www.anna-jersey.com/nhl-jerseys-c-882.html]NHL Jerseys China[/url],[url=www.anna-jersey.com/nba-jerseys-c-821.html]NBA Jerseys China[/url],[url=www.anna-jersey.com/mlb-jerseys-c-694.html]MLB Jerseys China[/url],[url=http://www.anna-jersey.com/]China Jerseys[/url],Free Shipping
[url=http://www.fitflop-uk.com/]fitflops sale UK[/url] [url=http://www.fitflop-uk.com/]fitflop sale[/url]
[url=http://www.china4jersey.com/]Wholesale Jerseys From China[/url],[url=http://www.china4jersey.com/]Wholesale Jerseys[/url],[url=http://www.china4jersey.com/]China Jerseys[/url],NFL Jerseys China Paypal,NHL Jerseys China Paypal,Chinese Jerseys factory,Sewn On Jerseys,Accept Paypal,Free Shipping