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.