Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5166 |  Welcome, Guest!   Register  Login
Home > Articles > Silverlight > Silverlight 4: How to Drag and Drop External Files?

Silverlight 4: How to Drag and Drop External Files?

Article posted by Kunal2383 on 3/3/2010 | Views: 9961 | Category: Silverlight | Level: Beginner red flag


In this post I will describe you another feature of Silverlight 4 “Access to external content”. Here I will show how to drag and drop some external images to my sample application. Earlier Silverlight 4 this feature was not available. There was no client file access permission. But in this new release they introduced this functionality by which you can implement the same.

Download


 Download source code for Silverlight 4: How to Drag and Drop External Files?


Introduction

In this post I will describe you another feature of Silverlight 4 “Access to external content”. Here I will show how to drag and drop some external images to my sample application. Earlier Silverlight 4 this feature was not available. There was no client file access permission. But in this new release they introduced this functionality by which you can implement the same.

Prerequisite
To implement this feature you must need Silverlight 4, which is now available in Beta 1 version. You need Visual Studio 2010 Beta 2 which you can download freely from Microsoft site.
Get Started

Now if your dev environment is ready then we can go further to implement the same. Excited so much to do it? Create a Silverlight project which will create “MainPage.xaml” for you. Inside the MainPage.xaml add a ScrollViewer containing a WrapPanel. Your ScrollViewer will have a fixed Height & Width where as your WrapPanel will be free size. This ensures that, if more components are added inside the WrapPanel it will automatically add a scrollbar to it. So, you can scroll through the child components. In this example I want to drop some external image files inside this panel. So, I will set the WrapPanel “AllowDrop” property to true. This will make the panel droppable.

Code Walk-through

On the Drop event handler of the wrap panel you will get the dropped files as data to the DropEventArgs which has an array of FileInfo. DataFormats.FileDrop sets the droppable permission to the panel.

FileInfo[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

Now for each dropped file you can check whether it is a supported image file. If so, proceed further to add it to the wrap panel. See the sample code: 

void imageContainer_Drop(object sender, DragEventArgs e)
{
FileInfo[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

foreach (FileInfo droppedFile in droppedFiles)
{
if (IsSupportedImageFile(droppedFile.Extension))
{
Border imagePlaceHolder = new Border()
{
Child = CreateImage(droppedFile),
Background = transparentColor,
Margin = new Thickness(10.0),
Cursor = Cursors.Hand,
};

ToolTipService.SetToolTip(imagePlaceHolder, droppedFile.Name);

imagePlaceHolder.MouseEnter += imagePlaceHolder_MouseEnter;
imagePlaceHolder.MouseLeave += imagePlaceHolder_MouseLeave;

imageContainer.Children.Add(imagePlaceHolder);
}
}
}

Here IsSupportedImageFile() method takes the extension of the dropped file as a parameter which will check whether it is a valid image format. I used .jpg & .png for the demonstration which actually uses switch case. The CreateImage() method creates an object of the image from the FileStream of the dropped file. 

private Image CreateImage(FileInfo droppedFile)
{
using (FileStream fileStream = droppedFile.OpenRead())
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(fileStream);

return new Image() { Source = bitmapImage, Width = 100, Margin = new Thickness(5.0) };
}
}


Conclusion

Now after writing this code, your application will be ready to get external file droppable inside it. Run your application & drop some jpg/png files from your image directory to your browser i.e. Silverlight application. You will see that the dropped images are automatically added inside your panel. This is a good example of accessing external files droppable inside your web application. Download the sample solution & implement what you want to do.


So what next? I think from the above example you will get the idea of what we can achieve from this. Anyway you can implement file upload utility by just dragging & dropping inside the web application just like Skydrive. You can also drop text files to read the file instead of browsing & uploading to the server. And more… Go ahead & enjoy programming with Silverlight 4.


Download Sample Application:   Silverlight 4 Drag-N-Drop External Image

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.kunal-chowdhury.com
Member since:Monday, March 01, 2010
Level:Starter
Status: [Member]
Biography:He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development
>> Write Response - Respond to this post and get points
Related Posts

Silverlight 4 supports local resource access by using COM API. In this article I will guide you to understand how we can call COM API to get the local drive, files and folder information. We will create a sample application to show you a basic File Explorer in Silverlight 4. After end of the article you will be able to understand it properly. Please don't forget to vote and leave feedbacks/suggestions.

In this article, we are going to learn about how to work with Services in Silverlight.

As you all know that, Out-Of-Browser functionality in Silverlight is not a new feature. It is available since Silverlight 3. Microsoft added the trusted application behaviour in Silverlight 4 Beta. In addition to this, they introduced one more additional functionality called “Chromeless OOB WIndow” i.e. “Customized OOB Window” in Silverlight 4 RC (Release Candidate). In this post, I will guide you to create a simple customized OOB Window Application in Silverlight 4 RC.

Telerik Assembly Minifier is a tool that lets you extract only the controls’ classes and resources you need to use in your application development, thus significantly reducing the size of the assemblies. Using the Assembly Minifier you will achieve significantly better loading time when the XAP files containing the minified (optimized) assemblies are to be loaded on the client side.

In this post I teach you how you can integrate JavasScript in your Silverlight apps. Sample source code available for download.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:55:28 AM