C# 带Windows窗体的WPF禁止拖放

C# 带Windows窗体的WPF禁止拖放,c#,wpf,winforms,drag-and-drop,windowsformshost,C#,Wpf,Winforms,Drag And Drop,Windowsformshost,我正在努力在WindowsFormsHost上进行拖放操作。当我从桌面上拖动文件时,Drop事件不会触发 我创建了一个示例项目来演示: 您需要参考WindowsFormsIntegration和System.Windows.Forms MainWindow.xaml <Window x:Class="WpfApp3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/p

我正在努力在
WindowsFormsHost
上进行拖放操作。当我从桌面上拖动文件时,
Drop
事件不会触发

我创建了一个示例项目来演示:

您需要参考
WindowsFormsIntegration
System.Windows.Forms

MainWindow.xaml

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        mc:Ignorable="d"
        AllowDrop="true"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <WindowsFormsHost 
                Background="red" 
                Height="200"
                AllowDrop="true"
                Drop="WindowsFormsHost_Drop">
                <wf:MaskedTextBox Height="200"  x:Name="mtbDate" Mask="00/00/0000"/>
            </WindowsFormsHost>
            <ListBox Name="LogList" />
        </StackPanel>
    </Grid>
</Window>

有人能告诉我我遗漏了什么吗?

我测试了一下这个qiute,我只能猜测这是由于WinForms和WPF的不兼容性造成的。根据我的研究,当这些技术受到欢迎时,事件并不总是在控制下流动

但我可以提出一些解决此限制的建议—将事件保留在WPF控件中,以便定义不可见的底层控件(它将占据与
WindowsFormsHost
完全相同的区域)来捕获事件:

<Grid>
    <Border
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        AllowDrop="True"
        Background="Transparent"
        Drop="WindowsFormsHost_Drop" />
    <WindowsFormsHost
        Name="wfh"
        Height="200"
        Background="red">
        <wf:MaskedTextBox
            x:Name="mtbDate"
            Height="200"
            Mask="00/00/0000" />
    </WindowsFormsHost>
</Grid>


我想你甚至可以尝试将
WindowsFormsHost
ishitestvisible
设置为
false
,这样所有的UI事件都会失败-但这取决于你自己测试并决定是否需要它。

非常感谢你的建议,它在我的示例应用程序中确实有效,但是我没有把它翻译成我的实际项目,因此我还没有把它标记为答案。仍在寻找一些其他答案
。将事件处理程序添加到WinForms控件。在
MtbDate\u DragEnter
中:
e.Effect=System.Windows.Forms.DragDropEffects.Copy
--您的
WindowsFormsHost\u Drop
中的
drageventarg
应该会给您一个不明确的引用异常,因为您需要在那里引用System.Windows.Forms。另外,您可以从
WindowsFormsHost
中删除
AllowDrop=“true”
<Grid>
    <Border
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        AllowDrop="True"
        Background="Transparent"
        Drop="WindowsFormsHost_Drop" />
    <WindowsFormsHost
        Name="wfh"
        Height="200"
        Background="red">
        <wf:MaskedTextBox
            x:Name="mtbDate"
            Height="200"
            Mask="00/00/0000" />
    </WindowsFormsHost>
</Grid>