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. I used the event in the grid, but no matter how I set the TargetObject or other property, the method won't be execute. However I made another project to reproduce the situation. Here is the code:
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. 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. 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
. So give it a name x:Name="MyMainWindow"
, and change your TargetObject
property.
<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.
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
). 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).
Assuming that drag source element implements what it should (you didn't put it in your XAML), your OnGrid_DragEnter
will be executed. If you need details on implementing Drag and Drop functionality, check Microsoft Docs.
上一篇: Firefox不尊重行