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.
Asked
Active
Viewed 1,240 times
4

Techie
- 77
- 6
-
Probably you can try implementing IDocHostUIHandler mentioned in http://stackoverflow.com/questions/20051892/webbrowser-dragdrop and then override the behavior. – AProgrammer Apr 08 '15 at 12:17
-
No. That does not help in my scenario. – Techie Apr 08 '15 at 12:42
2 Answers
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;
}
}
-
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: