Click here to Skip to main content
11,486,023 members (75,230 online)
Click here to Skip to main content

How to make any UI element drag-able using Behaviors in WPF

, 29 Aug 2010 CPOL 12.4K 2
Rate this:
Please Sign up or sign in to vote.
in order to make your UI element drag-able without any extra code, all what you have to do is just selecting behaviors from Assets panel in Expression Blend then drag and drop the MouseDragElementBehavior to your UI element.
for example we will do this to a rectangle here you are the XAML code:

<Rectangle Fill="Red" Stroke="Black" Margin="230,218,172,132">
<i:Interaction.Behaviors>
<il:MouseDragElementBehavior/>
</i:Interaction.Behaviors>
</Rectangle>


don't forget to include these namespaces:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"


if you are working with Blend, they will be placed automatically after setting the drag behavior, and if you are using Visual Studio you will need to set them manually

License

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

Share

About the Author

Ahmed_Said
Software Developer Asset Technology Group
Egypt Egypt
I'm a professional components designer, web developer, UX engineer and 3d designer as well, I'm 4 years experienced .net software engineer and 7 years experienced 3d designer using 3D Max. I'm very interested in RIA technologies, prototyping and UX engineering.

Ahmed Said
Senior .Net Software Engineer
Follow on   Twitter

Comments and Discussions

 
QuestionThank you Pin
André Canha1-Jul-12 8:02
memberAndré Canha1-Jul-12 8:02 
Generalthe drag behavior enables you to drag the element you want, ... Pin
Ahmed_Said31-Aug-10 2:34
memberAhmed_Said31-Aug-10 2:34 
GeneralIt will only allow you to drag a UI Element inside its conta... Pin
Simon Dufour30-Aug-10 3:09
memberSimon Dufour30-Aug-10 3:09 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 29 Aug 2010
Article Copyright 2010 by Ahmed_Said
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid