Archive

Archive for May, 2014

WPF POPUP control with Drag and Drop support and Resize support

May 27, 2014 Leave a comment

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: