Search code examples
wpfdrag-and-dropcursorflowdocument

Why I cannot drop files from explorer to FlowDocumentReader and how to fix it?


I'm trying to implement a piece of functionality that will let the user to drag files into an application to be opened in the FlowDocumentReader.

My problem is that is though I have AllowDrop=true on the FlowDocumentReader, the cursor does not change to the "drop here" icon but changes instead to "drop is not allowed" icon. This happens only to the FlowDocumentReader, all other parts og the UI (window itself, other controls) work as expected. The FlowDocumentReader actually receives the events, and it is possible to handle the drop, but the user does not have a visual indication that he can release the mouse here.

I also cannot hide the "drop is not allowed" cursor by setting Cursor=Cursors.None


Solution

  • Need to handle DragOver event in FlowDocument to allow dropping here.

    xaml:

    <!--
    <FlowDocumentReader x:Name="fdr" Background="White">
        <FlowDocument x:Name="doc" AllowDrop="True" DragEnter="doc_DragOver" Drop="doc_Drop" Background="White"/>
        </FlowDocumentReader>
    -->
    <FlowDocumentReader x:Name="fdr" Background="White">
       <FlowDocument x:Name="doc" AllowDrop="True" DragOver="doc_DragOver" Drop="doc_Drop" Background="White"/>
    </FlowDocumentReader>
    

    code behind:

    private void doc_DragOver(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.All;
        e.Handled = true;
    }
    
    private void doc_Drop(object sender, DragEventArgs e)
    {
    }