Click here to Skip to main content
Click here to Skip to main content

Draggable Popup Window in Silverlight 3

By , 19 Nov 2009
 

Introduction

In Silverlight 3, you can use a Popup control to show a non-modal window containing any sort of content. Although you can control the Popup's position by setting the HorizontalOffset and VerticalOffset properties, it can sometimes be hard to predict where the user would like the window to be positioned. Unfortunately, the user has no way to move the Popup control.

By attaching a PopupDragDrop object to your Popup control, you allow the user to drag it around the Silverlight window and place it wherever she likes.

Using the code 

Simply call the static PopupDragDrop.Attach method to wire up an instance of the class to your Popup. Attach takes two arguments: the Popup control and the FrameworkElement that will be the Popup's child (which holds the actual content that will be displayed). For example:

// Create the Popup control and set the initial position.	
Popup myPopup = new Popup { HorizontalOffset = 200, VerticalOffset = 300 };

// Attach a PopupDragDrop object to make your Popup draggable.
// The Attach method will also set the Child property of the Popup.
PopupDragDrop.Attach(myPopup, new myUserControl());	

From this point, you show/hide the Popup in the usual way, using the IsOpen property of the Popup and the Visibility property of the child.

History

  • 11/19/2009 - Initial contribution.

License

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

About the Author

dsmolen
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberricoeur4228 Mar '13 - 6:37 
Simple, clean and efficient. thx
GeneralMy vote of 5memberAaronBastian28 Nov '12 - 2:14 
This worked flawlessly, and was as simple to implement as was in the example. Killer job!
Generalwhat is myUserControl() ?memberlevashoff27 Apr '11 - 9:37 
what is myUserControl() ?
GeneralRe: what is myUserControl() ?memberdsmolen27 Apr '11 - 11:27 
myUserControl is a FrameworkElement that defines the content of the Popup. PopupDragDrop.Attach will set the Child property of the popup to its 2nd argument.
 
So, myUserControl is just whatever you would be setting the Child property of your Popup to.
 
HTH
GeneralMy vote of 5membersintesia4 Apr '11 - 3:22 
Without frills and time-wasting eloquency narrating the history of information technology, this article shows a quick usable way to make the thing work.
GeneralCongratsmemberviv28122 Jan '10 - 4:44 
It is great program. I have one question How can I attach drag functionality to only header section of Window.
At present this program allows Dragging from every where.
 
Vivek
GeneralRe: Congratsmemberdsmolen2 Jan '10 - 7:03 
You would have to modify the code to capture mouse events just for the header. The best approach would be to start from scratch using my code for ideas.
 
Thanks for the kind words. Good luck.
GeneralMore Description NeededmvpAbhijit Jana19 Nov '09 - 17:15 
This can't be approve as an Article. You can provide some more explanation, code description and Screen shots .
 
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Nov 2009
Article Copyright 2009 by dsmolen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid