DragDropManager get KeyPress during Dragging

1 Answer 44 Views
DragAndDrop
Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
Benedikt asked on 04 Oct 2024, 12:06 PM

Hey,

I am using a DragDropManager to drag images from a itemscontrol onto a table.
Now I want to rotate the image, if the user presses the Key "R" during Dragging.

But I dont get it to work.
All Input Bindings on the Window and DataTemplate did not fire.
Does anyone have an idea to achieve what I want?

DragDropManager.AddDragInitializeHandler(icImages, OnDragInitialize);
private void OnDragInitialize(object sender, DragInitializeEventArgs e)
        {
          
            clsImage imageData = (clsImage)((FrameworkElement)e.OriginalSource).DataContext;

            DragDropData dragData = new DragDropData
            {
                ImageData = imageData ,
                Column = Column
            };

            e.Data = dragData;
            e.DragVisual = new ContentControl { ContentTemplate = LayoutRoot.Resources["ImageTemplate"] as DataTemplate, Content = imageData };
            e.AllowedEffects = DragDropEffects.All;

            e.Handled = true;
        }
            <DataTemplate x:Key="ImageTemplate">
                <Grid>
                    <telerik:RadSvgImage
                        UriSource="{Binding file}"
                        HorizontalAlignment="Left"
                        SizeType="ContentToSizeStretch"
                        OverrideColor="{Binding ImageColor}"
                        />
                </Grid>
            </DataTemplate>

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 09 Oct 2024, 11:25 AM

Hello Benedikt,

When a drag operation is undergoing, the DragVisual element is hosted in a native WPF Window element, which when dragged will not raise any keyboard events.

With this in mind, such a behavior can be achieved with a bit of custom code, as well as attaching a global keyboard hook, in order to intercept when a keyboard key is pressed while dragging.

The following StackOverflow's thread contains a code snippet about the global keyboard hook:

Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C# - Stack Overflow

I have attached a test application, which contains the implementation of the above suggestions. 

The produced behavior is as follows:

I hope the provided information and example will be of help to you.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
commented on 10 Oct 2024, 11:57 AM

Hi Stenly,

great explanation and thank you for your example.
This works great and does exactly what I wanted.

Thank you very much.
Greetings

Tags
DragAndDrop
Asked by
Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or