简体   繁体   English

如何禁用WPF Web浏览器控件上的删除

[英]How to disable drop on wpf webbrowser control

How can I disable dropping of files on a System.Windows.Controls.WebBrowser? 如何禁用System.Windows.Controls.WebBrowser上的文件删除? From the documentation it would seem that this behaviour should be disabled by default as it is inherited from the UIElement.AllowDrop Property. 从文档看来,默认情况下应该禁用此行为,因为它是从UIElement.AllowDrop属性继承的。

However dy default I can drag and drop files on to a WebBrowser control. 但是,默认情况下,我可以将文件拖放到WebBrowser控件上。 Further to this it seems I can't disable this supposedly none-default behaviour. 除此之外,似乎我无法禁用此所谓的非默认行为。 For example if I explicitly set the value of the property to false in XAML 例如,如果我在XAML中将属性的值显式设置为false

<WebBrowser Name="webBrowser1" AllowDrop="False" />

..and/or in code-behind, ie ..和/或隐藏在代码中,即

webBrowser1.AllowDrop = false;

Then I can still drag and drop files onto the control. 然后,我仍然可以将文件拖放到控件上。 How can I disable this behaviour and remove the security risk it creates? 如何禁用此行为并消除它造成的安全风险?

Ok after hours and hours of playing with this I have come up with one solution that works. 经过数小时的努力,我想出了一个可行的解决方案。 Because I am using the standard WPF webbrowser I know I can also use the "Extended Event Attributes" that Microsoft introduced for Internet Explorer. 因为我使用的是标准WPF Web浏览器,所以我知道我也可以使用Microsoft为Internet Explorer引入的“扩展事件属性”。

The event I am using to disable dropping files onto the control is ondragover. 我用于禁用将文件拖放到控件上的事件是ondragover。 Essentially I just cancel the event whenever it happens like so. 本质上,我只是在事件发生时就取消它。

<body ondragover="window.event.returnValue=false;">

This is not really ideal - but what is good about this technique is that it allows for a whole host of other properties to be set that are not available directly from managed code. 这并不是真正理想的方法-但是此技术的优点在于,它允许设置许多其他属性,这些属性不能直接从托管代码中获得。 For the scrollbar state, which is not exposed in managed code, can be set this way. 对于滚动条状态(未在托管代码中公开),可以通过这种方式设置。

<body scroll="no">

I tried a couple of things, but it looks like the WebBrowser control asserts its primacy over and above anything the layout engine wants. 我尝试了几件事,但看起来WebBrowser控件在布局引擎想要的任何东西之上都断言了其首要地位。 It doesn't obey or even really sit in the visual tree, except for control siting. 除了控制选址外,它不服从甚至不真正位于视觉树中。 Panel.ZIndex had no effect, etc. Panel.ZIndex无效,等等。

Perhaps a different HTML layout control would be in order, one that behaves better than the ActiveX IE 6/7/8 interface here: 也许会按顺序使用不同的HTML布局控件,该控件的行为比ActiveX IE 6/7/8接口好:

Replacing .NET WebBrowser control with a better browser, like Chrome? 用更好的浏览器(例如Chrome)替换.NET WebBrowser控件?

There is a little bit tricky way to do this by Reflection: 通过反射有一些棘手的方法:

1.) Get the underlaying browser COM instance (SHDocVw.IWebBrowser2) by invoking the getter on non public instance property "AxIWebBrowser2". 1.)通过调用非公共实例属性“ AxIWebBrowser2”上的吸气剂,获得底层浏览器COM实例(SHDocVw.IWebBrowser2)。

2.) Set property "RegisterAsDropTarget" to false on browser COM object also by invoking the property setter. 2.)也可以通过调用属性设置器,在浏览器COM对象上将属性“ RegisterAsDropTarget”设置为false。

That's all. 就这样。

var webBrowser = this.WebBrowser.GetType().InvokeMember("AxIWebBrowser2",
                                                        BindingFlags.Instance |
                                                        BindingFlags.NonPublic |
                                                        BindingFlags.GetProperty,
                                                        null,
                                                        this.WebBrowser,
                                                        new Object[] { });

webBrowser.GetType().InvokeMember("RegisterAsDropTarget",
                                  BindingFlags.Instance |
                                  BindingFlags.SetProperty,
                                  null,
                                  webBrowser,
                                  new Object[] { false });

Hint: "this.WebBrowser" is your WPF WebBrowser instance (System.Windows.Controls.WebBrowser). 提示:“ this.WebBrowser”是您的WPF WebBrowser实例(System.Windows.Controls.WebBrowser)。

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

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