【问题标题】:WPF Webbrowser control disable dropWPF Webbrowser 控件禁用删除
【发布时间】:2015-06-13 08:46:05
【问题描述】:

有没有办法禁止将记事本、word 等项目放到 WPF 中的 System.Windows.Controls.WebBrowser 控件上。 我尝试了各种选项,例如 AllowDrop="False" - 不起作用。 试图捕捉 Drop、PreviewDrop、DragLeave、PreviewDragLeave、PreviewDragOver、PreviewDragEnter 等事件 - 当我将项目放在 WebBrowser 控件上时,这些事件都不会触发。

【问题讨论】:

标签: c# wpf drag-and-drop wpf-controls webbrowser-control


【解决方案1】:

使用构造函数或 Xaml 创建以下方法将阻止拖动更改网络浏览器当前状态:

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

【讨论】:

  • 信息:if (!e.Uri.IsFile) MailManager.FileManager.OpenExternalLink (e.Uri) - 使用“IsFile”将本地拖放提供的文件路径与实际单击或导航的超链接区分开来。还要确保检查if (e.Uri != null),因为如果它为空,则导航可能是正常加载 HTML 代码或任何其他预期导航。 e.Cancel = true; 将取消任何尝试的导航。
【解决方案2】:

正如您所提到的,没有任何 Drop 事件被触发,而且本地浏览器控件似乎非常有限,这给您留下了两种可能性:

  • 用户使用另一个浏览器控件,例如wpfchromium

  • 或者在浏览器顶部定义一个透明弹窗并处理 甚至在里面的水滴

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

这个解决方案一点也不漂亮,还需要更多逻辑 handle when the popup's parent control moves..

...如果您的主要内容是 html,您也可能会发现此解决方案很有帮助:

How to disable drop on wpf webbrowser control

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 2013-12-30
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2010-11-28
    相关资源
    最近更新 更多