#730 – Use QueryContinueDrag Event to Know When Mouse Button State Changes

The QueryContinueDrag event lets you know when a mouse button changes during a drag-drop operation.  It will also indicate whether the state of the Shift, Ctrl, or Alt keys change while dragging.  You wire up an event handler to the control that the drag-drop operation originates from.

In the example below, the source control waits for the left mouse button to be released and then cleans out its content.

        <Label Content="Drag from here" Background="LavenderBlush"
               HorizontalAlignment="Center" Margin="10" Padding="10"
               MouseDown="Label1_MouseDown"
               QueryContinueDrag="Label1_QueryContinueDrag"/>
        <Label Content="To here" Background="SandyBrown" AllowDrop="True"
               HorizontalAlignment="Center" Margin="10" Padding="10"
               Drop="Label2_Drop"/>
        private void Label1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label lblFrom = e.Source as Label;

            if (e.LeftButton == MouseButtonState.Pressed)
                DragDrop.DoDragDrop(lblFrom, lblFrom.Content, DragDropEffects.Copy);
        }

        private void Label1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            Label lblFrom = e.Source as Label;

            if (!e.KeyStates.HasFlag(DragDropKeyStates.LeftMouseButton))
                lblFrom.Content = "...";
        }

        private void Label2_Drop(object sender, DragEventArgs e)
        {
            string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);

            Label toLabel = e.Source as Label;
            toLabel.Content = draggedText;
        }

730-001
730-002

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: