5,121,238 members and growing! (14,855 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Intermediate License: The Code Project Open License (CPOL)

Implementing Drag Drop Operations for Browser Based WPF Applications (XBAP)

By Nick Polyak

Simple implementation of Drag and Drop operation without full trust requirement
C# (C# 3.0, C#), Windows (Windows, Win2K, WinXP, Win2003, Vista), .NET (.NET, .NET 3.5, .NET 3.0), WPF, Arch, Dev

Posted: 16 Mar 2008
Updated: 16 Mar 2008
Views: 3,196
Announcements



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 1.10 Rating: 3.67 out of 5
1 vote, 50.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
1 vote, 50.0%
5

Introduction

In the article, "Very simple WPF Drag and Drop Sample without Win32 Calls", I stated that because of not using Win32 calls, the resulting code should also work in the browser under partial trust. Well... I was wrong. It turned out that DragDrop.DoDragDrop(...) function would throw a SecurityException in an XBAP application under partial trust since DragDrop.DoDragDrop originates a so called OLE Drag'n Drop operation. For most applications, when we are dragging and dropping only WPF objects, no OLE transfer is necessary. So in this article, I give an example of simulating the Drag'n Drop functionality without DragDrop.DoDragDrop method that runs in a browser.

Using the Code

To use the code, simply unzip the file, start the project, compile and run the application.

There are two interesting things I had to figure out in order to make the application work in a browser: making sure that the picture of the cursor is visible and adopts a shape that we want. Both are implemented in ListView1_MouseMove callback for ListView1.MouseMove event.

To make the cursor visible, we simply do:

Mouse.SetCursor(Cursors.Hand);        

There is a problem that remains, however: I wanted the cursor to become a rectangle and there were no rectangle shapes among Cursors enumeration. And to top it all, you cannot use a bitmap image for a cursor in an XBAP partial trust application. To get around this problem, I made the cursor to be a semi-transparent rectangle visible only for the duration of the drag operation:

<Rectangle
    Name="CursorRectangle"
    Height="10" 
    Width="20" 
    Visibility="Hidden">
    <Rectangle.Fill>
        <SolidColorBrush Color="Blue" 
             Opacity="0.35"/>
    </Rectangle.Fill>
</Rectangle>

The following code ensures that the cursor rectangle does not move outside of the ListView control boundaries:

Point p = e.GetPosition(ListView1); // get the location of the mouse pointer

// get the boundaries of the ListView control (the cursor should not 
// be allowed to go beyond those boundaries
Rect bounds = VisualTreeHelper.GetDescendantBounds(ListView1); 

// set the vertical coordinate of the cursor to 
// coincide with the current mouse vertical coordinate
// as long as we are still within the boundaries of the 
// ListView control
if ( (bounds.Top < p.Y) && (bounds.Bottom > p.Y))
{
    Canvas.SetTop(CursorRectangle, p.Y);
}

// set the vertical coordinate of the cursor to 
// coincide with the current mouse horizontal coordinate
// as long as we are still within the boundaries of the 
// ListView control
if ((bounds.Left < p.X) && (bounds.Right > p.X))
{
    Canvas.SetLeft(CursorRectangle, p.X);
}

The rest of the code is very similar to that of "Very simple WPF Drag and Drop Sample without Win32 Calls" article.

History

  • 16th March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nick Polyak



Occupation: Software Developer (Senior)
Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Nick Polyak
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project