4

Is there a way to disable dropping items like notepad, word etc on to a System.Windows.Controls.WebBrowser control in WPF. I have tried various options like AllowDrop="False" - Does not work. Tried to catch events like Drop, PreviewDrop, DragLeave, PreviewDragLeave, PreviewDragOver, PreviewDragEnter - None of these events fire when I drop items on the WebBrowser control.

Techie
  • 77
  • 6

2 Answers2

2

Using the constructor or Xaml to create the following method will stop the drag from changing the webbrowsers current state:

private void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
    // The WebBrowser control is checking the Uri 
    if (e.Uri.ToString() != "Place your url string here") //ex: "http://stackoverflow.com"
    {
        // Uri is not the same so it cancels the process
        e.Cancel = true;
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Devdude
  • 99
  • 7
  • Info: `if (!e.Uri.IsFile) MailManager.FileManager.OpenExternalLink (e.Uri)` - Use "IsFile" to differentiate local drag & drop provided file paths from actually clicked or navigated hyperlinks. Also make sure to check `if (e.Uri != null)`, because if it is null, the navigation might be a normal loading of HTML-code or any other intended navigation. `e.Cancel = true;` will cancel any navigation that is attempted. – Battle Oct 30 '19 at 06:27
0

As you mentioned, none of the Drop event are fired, and it seems like the native browser control is very limited which leaves you with two possibilities:

  • Either user another browser control like wpfchromium,

  • Or define a transparent popup in top of the browser and handle the drop even within it

:

<WebBrowser Height="300" Width="300" x:Name="wbBrowser"/>
    <Popup  Opacity="1" AllowsTransparency="True" IsOpen="True" Placement="Center" 
     PlacementTarget="{Binding ElementName=wbBrowser}" >
        <Border  Background="Black" Opacity="0.01" 
         Width="300" Height="300">
        </Border>
    </Popup>

This solution ain't pretty at all and needs also more logic to handle when the popup's parent control moves..

...you may also find this solution helpful, if your main content is html:

How to disable drop on wpf webbrowser control

Community
  • 1
  • 1
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47