简体   繁体   English

WPF Web浏览器控件禁用删除

[英]WPF Webbrowser control disable drop

Is there a way to disable dropping items like notepad, word etc on to a System.Windows.Controls.WebBrowser control in WPF. 有没有办法禁止像WPF中的System.Windows.Controls.WebBrowser控件那样删除记事本,单词等项目。 I have tried various options like AllowDrop="False" - Does not work. 我尝试了各种选项,如 AllowDrop="False" - 不起作用。 Tried to catch events like Drop, PreviewDrop, DragLeave, PreviewDragLeave, PreviewDragOver, PreviewDragEnter - None of these events fire when I drop items on the WebBrowser control. 尝试捕获Drop,PreviewDrop,DragLeave,PreviewDragLeave,PreviewDragOver,PreviewDragEnter等事件 - 当我在WebBrowser控件上删除项时,这些事件都不会触发。

Using the constructor or Xaml to create the following method will stop the drag from changing the webbrowsers current state: 使用构造函数或Xaml创建以下方法将阻止拖动更改webbrowsers当前状态:

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;
    }
}

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: 正如您所提到的,Drop事件都没有被触发,看起来本机浏览器控件非常有限,这使您有两种可能:

  • Either user another browser control like wpfchromium , 用户另一个浏览器控件,如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: ...如果你的主要内容是html,你也可能会发现这个解决方案很有帮助:

How to disable drop on wpf webbrowser control 如何禁用wpf webbrowser控件上的drop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM