简体   繁体   English

WPF CallMethodAction在Drop,DragOver,DragEnter事件中不起作用

[英]WPF CallMethodAction doesn't work in Drop, DragOver, DragEnter event

I used MVVM for my wpf project, and want to move the event( Drop, DragOver, DragEnter) to my viewmodel. 我将MVVM用于我的wpf项目,并想将事件(Drop,DragOver,DragEnter)移动到我的视图模型中。 I used the event in the grid, but no matter how I set the TargetObject or other property, the method won't be execute. 我在网格中使用了该事件,但是无论我如何设置TargetObject或其他属性,该方法都不会执行。 However I made another project to reproduce the situation. 但是我做了另一个项目来重现这种情况。 Here is the code: 这是代码:

XAML XAML

<Window x:Class="WpfApplication2.MainWindow"
                  .
                  .
                  .
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="grid" AllowDrop="True" >
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="110,57,0,0"/>
        <i:Interaction.Triggers>
            <!--<i:EventTrigger EventName="Drop">
                <ie:CallMethodAction MethodName="OnGrid_Drop" TargetObject="{Binding}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="DragOver">
                <ie:CallMethodAction MethodName="OnGrid_DragOver" TargetObject="{Binding }" />
            </i:EventTrigger>-->
            <i:EventTrigger EventName="DragEnter">
                <ie:CallMethodAction MethodName="OnGrid_DragEnter" TargetObject="{Binding ElementName=grid}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>

CodeBehind 代码背后

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            AllocConsole();
        }

        private void OnGrid_Drop(object sender, DragEventArgs e)
        {
            Console.WriteLine("00000");          
        }
        private void OnGrid_DragEnter(object sender, DragEventArgs e)
        {
            Console.WriteLine("11111");      
        }
        private void OnGrid_DragOver(object sender, DragEventArgs e)
        {
            Console.WriteLine("22222");         
        }

        ...... //The code in here is to make the console pop up.
    }

I have two question : 我有两个问题:

(1) I drag something over the grid, the method wasn't be executed. (1)我在网格上拖了一些东西,该方法没有执行。 When the mouth move over the button, the mouse cursor did change, but the method wasn't be executed,either. 当鼠标指针移到按钮上时,鼠标光标确实发生了变化,但是该方法也未执行。 Why is that? 这是为什么?

(2) If change grid property to <Grid x:Name="grid" AllowDrop="True" DragEnter="OnGrid_DragEnter"> (and delete the Interaction.Triggers), only when I drag something on the button, the event would be triggered. (2)如果将grid属性更改为<Grid x:Name="grid" AllowDrop="True" DragEnter="OnGrid_DragEnter"> (并删除Interaction.Triggers),则仅当我在按钮上拖动某些内容时,事件才会发生触发。 I pretty sure my mouse is in the grid, but only trig when mouse over the button. 我很确定我的鼠标在网格中,但是只有当鼠标悬停在按钮上时才触发。 Why is that? 这是为什么?

The code is very easy to reproduce, hope anyone help me to fix this problem. 该代码很容易重现,希望有人能帮助我解决此问题。

TargetObject 目标对象

It is the object that exposes the method. 公开该方法的是对象。 And that is not the Grid named "grid", it is your MainWindow . 那不是名为“ grid”的Grid格,而是您的MainWindow So give it a name x:Name="MyMainWindow" , and change your TargetObject property. 因此,给它一个名称x:Name="MyMainWindow" ,并更改您的TargetObject属性。

<i:EventTrigger EventName="DragEnter">
    <ie:CallMethodAction MethodName="OnGrid_DragEnter" //
                         TargetObject="{Binding ElementName=MyMainWindow}" />
</i:EventTrigger>

Method accessibility 方法可访问性

OnGrid_DragEnter is not called from within your MainWindow class, so it has to become public. 不会从MainWindow类中调用OnGrid_DragEnter ,因此必须将其公开。

public partial class MainWindow : Window
{
    ....
    public void OnGrid_DragEnter(object sender, DragEventArgs e)
    {
        Console.WriteLine("11111");         
    }
    ....
}

Set Grid's Background 设置网格的背景

You need to set Background property on the Grid to be able to track mouse events (that is why it triggers only when mouse is over the Button ). 您需要在Grid上设置Background属性,以能够跟踪鼠标事件(这就是为什么仅当鼠标悬停在Button时才触发的原因)。 If it needs to be transparent, you can set it to Transparent (but watch out later, because if you have an element underneath the grid, you might end up wondering why that element doesn't react to events like mouse click). 如果需要透明,则可以将其设置为“ Transparent (但是请稍后注意,因为如果网格下方有一个元素,则可能最终想知道为什么该元素对鼠标单击之类的事件没有反应)。

Assuming that drag source element implements what it should (you didn't put it in your XAML), your OnGrid_DragEnter will be executed. 假设拖动源元素实现了应有的功能(您未将其放入XAML中),则将执行OnGrid_DragEnter If you need details on implementing Drag and Drop functionality, check Microsoft Docs . 如果您需要有关实现拖放功能的详细信息,请检查Microsoft Docs

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

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