Home > Prsnl References > WPF POPUP control with Drag and Drop support and Resize support

WPF POPUP control with Drag and Drop support and Resize support

To enable DragDrop support follow the steps

Add namespace System.Windows.Interactivity; in WPF project

Creates a class like this

public class MouseDragPopupBehavior : Behavior<Popup>
    {
        private bool mouseDown;
        private Point oldMousePosition;

        protected override void OnAttached()
        {
            AssociatedObject.MouseLeftButtonDown += (s, e) =>
            {
                mouseDown = true;
                oldMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
                AssociatedObject.Child.CaptureMouse();
            };
            AssociatedObject.MouseMove += (s, e) =>
            {
                if (!mouseDown) return;
                var newMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
                var offset = newMousePosition – oldMousePosition;
                oldMousePosition = newMousePosition;
                AssociatedObject.HorizontalOffset += offset.X;
                AssociatedObject.VerticalOffset += offset.Y;
            };
            AssociatedObject.MouseLeftButtonUp += (s, e) =>
            {
                mouseDown = false;
                AssociatedObject.Child.ReleaseMouseCapture();
            };
        }
    }

IN Xaml redesign popup like this

 xmlns:myControl=”clr-namespace:use ur name space”
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity&#8221;

 

And design popup like

   <Popup………………….>
                <i:Interaction.Behaviors>
                    <myControl:MouseDragPopupBehavior/>
                </i:Interaction.Behaviors>   
                    …..  Fil popup contents here….     
            </Popup>

To support resize  we need to add few more things .

First redesign popup like

<Popup   Name=”popupname” …>
                <i:Interaction.Behaviors>
                    <myControl:MouseDragPopupBehavior/>
                </i:Interaction.Behaviors>
                <Grid>
                    —-popup contents—
                    <Thumb HorizontalAlignment=”Right” VerticalAlignment=”Bottom” Width=”16″ Height=”16″
                              DragStarted=”onDragStarted” DragDelta=”onDragDelta” DragCompleted=”onDragCompleted”/>
                </Grid>
            </Popup>

 

And in c# write events

  private void onDragStarted(object sender, DragStartedEventArgs e)
        {
            Thumb t = (Thumb)sender;
            t.Cursor = Cursors.Hand;
        }

        private void onDragDelta(object sender, DragDeltaEventArgs e)
        {

            double yadjust = this.popupname.Height + e.VerticalChange;
            double xadjust = this.popupname.Width + e.HorizontalChange;
            if ((xadjust >= 0) && (yadjust >= 0))
            {
                this.popupname.Width = xadjust;
                this.popupname.Height = yadjust;
            }
        }

        private void onDragCompleted(object sender, DragCompletedEventArgs e)
        {
            Thumb t = (Thumb)sender;
            t.Cursor = null;
        }

Advertisements
Categories: Prsnl References Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: