none
Drag and Drop issue with DataGrid RRS feed

  • Вопрос

  • Hi guys,

    I am facing an issue and wonder how to fix.

    My requirement is to allow dragging files to a datagrid, but prevent dragging folders.

    So I add DataGrid_PreviewDragOver event to datagrid and make e.Handled = false when the dragged object is folder. But it doesn't work, the drag event will still execute even the e.Handled is set to false.

    To simplify the issue, I created a new wpf app and add a datagrid to the view.

    <DataGrid AllowDrop="True" PreviewDragOver="DataGrid_PreviewDragOver"/>
    private void DataGrid_PreviewDragOver(object sender, DragEventArgs e)
            {
                e.Handled = true;
            }

    Then I ran the app, drag should be banned in expectation, but I can still drag objects to the datagrid and if I add a Drop event on the grid, it will always be triggered.

    Any suggestion?


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.


    20 августа 2019 г. 9:25

Ответы

  • Hi Yong,

    Thank you for your reply.

    I figured it out by using 

    e.Effects = DragDropEffects.None;                e.Handled = true;
    What makes me confused is that when I set e.Handled to false in textbox's preview event, the textbox can not receive dragged objects. But to the opposite, I need to set Handled to true and Effects to None to block dragging for datagrid.

    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    • Помечено в качестве ответа Justin Liu_FoxDaveMVP 26 августа 2019 г. 1:22
    22 августа 2019 г. 4:37

Все ответы


  • Hi Justin,

    Derive from Drop Target Events

    Drop: This event occurs when an object is dropped on the drop target. This is a bubbling event.
    DragOver: This event occurs continuously while an object is dragged (moved) within the drop target's boundary. This is a bubbling event.

    >drag should be banned in expectation ,but I can still drag objects to the datagrid and if I add a Drop event on the grid, it will always be triggered.

    e.Handled = true  in the method DataGrid_PreviewDragOver indicates that the event PreviewDragOver is to be marked handled ,and its corresponding bubbling event DragOver will not be implemented. Drop's corresponding tunneling event is PreviewDrop. There seems to be no dependency between events Drop and DragOver.

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    22 августа 2019 г. 2:30
    Модератор
  • Hi Yong,

    Thank you for your reply.

    I figured it out by using 

    e.Effects = DragDropEffects.None;                e.Handled = true;
    What makes me confused is that when I set e.Handled to false in textbox's preview event, the textbox can not receive dragged objects. But to the opposite, I need to set Handled to true and Effects to None to block dragging for datagrid.

    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    • Помечено в качестве ответа Justin Liu_FoxDaveMVP 26 августа 2019 г. 1:22
    22 августа 2019 г. 4:37
  • Hi Yong,

    Thank you for your reply.

    I figured it out by using 

    e.Effects = DragDropEffects.None;                e.Handled = true;
    What makes me confused is that when I set e.Handled to false in textbox's preview event, the textbox can not receive dragged objects. But to the opposite, I need to set Handled to true and Effects to None to block dragging for datagrid.

    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.


    Hi Justin,

    Glad to know your trigger problem is solved.  The textBox you mentioned later cannot receive dragged object , could you give detailed description for it? Is there some event that can’t be triggered or receive no data after it is triggered?

    Besides, I suggest you can start a new thread when you have a new question.
     

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    23 августа 2019 г. 10:11
    Модератор