During a drag in Wpf, how can the mouse cursor (or perhaps using an adorner) be changed to indicate that the droptarget
will not accept the dragged item?
在Wpf中拖动时,如何更改鼠标光标(或者可能使用装饰器)以指示droptarget不接受拖动的项目?
I've tried to set e.Effects = DragDropEffects.None
during the DragEnter
event but this isn't working and I suspect that I've misunderstood what that feature should be used for. I've tried using the GiveFeedback
event but don't see how the droptarget
can influence it.
我试图在DragEnter事件期间设置e.Effects = DragDropEffects.None,但这不起作用,我怀疑我误解了应该使用的功能。我已经尝试过使用GiveFeedback事件,但是没有看到droptarget如何影响它。
Are there any tutorials that cover rejection by the droptarget
in Wpf?
是否有任何教程可以解决Wpf中droptarget的拒绝问题?
3 个解决方案
#1
6
Just setting the DragDropEffects in DragEnter of the drop target should work. Is your DragEnter even getting called. Have you set AllowDrop on the drop target control?
只需在放置目标的DragEnter中设置DragDropEffects即可。你的DragEnter甚至被调用了吗?您是否在放置目标控件上设置了AllowDrop?
This is the sequence of events during a drag & drop in WPF (taken from MSDN) which might help work out what's going on...
这是WPF拖放过程中的一系列事件(取自MSDN),这可能有助于解决正在发生的事情......
-
Dragging is initiated by calling the DoDragDrop method for the source control.
通过调用源控件的DoDragDrop方法启动拖动。
The DoDragDrop method takes two parameters: * data, specifying the data to pass * allowedEffects, specifying which operations (copying and/or moving) are allowed
DoDragDrop方法有两个参数:* data,指定要传递* allowedEffects的数据,指定允许哪些操作(复制和/或移动)
A new DataObject object is automatically created.
将自动创建一个新的DataObject对象。
- This in turn raises the GiveFeedback event. In most cases you do not need to worry about the GiveFeedback event, but if you wanted to display a custom mouse pointer during the drag, this is where you would add your code.
- Any control with its AllowDrop property set to True is a potential drop target. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event.
- As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.
- If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.
这反过来会引发GiveFeedback事件。在大多数情况下,您不必担心GiveFeedback事件,但如果您想在拖动过程中显示自定义鼠标指针,则可以在此处添加代码。
AllowDrop属性设置为True的任何控件都是潜在的放置目标。 AllowDrop属性可以在设计时在“属性”窗口中设置,也可以在Form_Load事件中以编程方式设置。
当鼠标经过每个控件时,将引发该控件的DragEnter事件。 GetDataPresent方法用于确保数据的格式适合目标控件,Effect属性用于显示相应的鼠标指针。
如果用户在有效的放置目标上释放鼠标按钮,则会引发DragDrop事件。 DragDrop事件处理程序中的代码从DataObject对象中提取数据并将其显示在目标控件中。
#2
0
I had a similar problem because I changed the cursor in the GiveFeedback handler. This cursor was used even the drop target did reject the data. After switching back to the default cursor (e.UseDefaultCursors = true) the cursor shape did change propely to "not allowed".
我遇到了类似的问题,因为我更改了GiveFeedback处理程序中的光标。即使丢弃目标拒绝数据,也使用此游标。切换回默认光标(e.UseDefaultCursors = true)后,光标形状确实改变为“不允许”。
#3
0
You didn't say if you use the DragOver
even. Maybe you're setting e.Effect = DragDropEffects.All;
in this even and it will be fired repeatedly after you enter the target control instead of DragEnter
that will be fired just once.
你没有说你是否使用DragOver。也许你正在设置e.Effect = DragDropEffects.All;在这个偶数中,它将在您输入目标控件后重复触发,而不是只触发一次的DragEnter。
private void arbol_DragOver(object sender, DragEventArgs e)
{
if (some_reason)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.All;
}
If you didn't use this event or didn't modify e.Effect
within, then it's hard to say. Code is needed.
如果您没有使用此事件或没有修改内部的e.Effect,那么很难说。代码是必需的。
#1
6
Just setting the DragDropEffects in DragEnter of the drop target should work. Is your DragEnter even getting called. Have you set AllowDrop on the drop target control?
只需在放置目标的DragEnter中设置DragDropEffects即可。你的DragEnter甚至被调用了吗?您是否在放置目标控件上设置了AllowDrop?
This is the sequence of events during a drag & drop in WPF (taken from MSDN) which might help work out what's going on...
这是WPF拖放过程中的一系列事件(取自MSDN),这可能有助于解决正在发生的事情......
-
Dragging is initiated by calling the DoDragDrop method for the source control.
通过调用源控件的DoDragDrop方法启动拖动。
The DoDragDrop method takes two parameters: * data, specifying the data to pass * allowedEffects, specifying which operations (copying and/or moving) are allowed
DoDragDrop方法有两个参数:* data,指定要传递* allowedEffects的数据,指定允许哪些操作(复制和/或移动)
A new DataObject object is automatically created.
将自动创建一个新的DataObject对象。
- This in turn raises the GiveFeedback event. In most cases you do not need to worry about the GiveFeedback event, but if you wanted to display a custom mouse pointer during the drag, this is where you would add your code.
- Any control with its AllowDrop property set to True is a potential drop target. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event.
- As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.
- If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.
这反过来会引发GiveFeedback事件。在大多数情况下,您不必担心GiveFeedback事件,但如果您想在拖动过程中显示自定义鼠标指针,则可以在此处添加代码。
AllowDrop属性设置为True的任何控件都是潜在的放置目标。 AllowDrop属性可以在设计时在“属性”窗口中设置,也可以在Form_Load事件中以编程方式设置。
当鼠标经过每个控件时,将引发该控件的DragEnter事件。 GetDataPresent方法用于确保数据的格式适合目标控件,Effect属性用于显示相应的鼠标指针。
如果用户在有效的放置目标上释放鼠标按钮,则会引发DragDrop事件。 DragDrop事件处理程序中的代码从DataObject对象中提取数据并将其显示在目标控件中。
#2
0
I had a similar problem because I changed the cursor in the GiveFeedback handler. This cursor was used even the drop target did reject the data. After switching back to the default cursor (e.UseDefaultCursors = true) the cursor shape did change propely to "not allowed".
我遇到了类似的问题,因为我更改了GiveFeedback处理程序中的光标。即使丢弃目标拒绝数据,也使用此游标。切换回默认光标(e.UseDefaultCursors = true)后,光标形状确实改变为“不允许”。
#3
0
You didn't say if you use the DragOver
even. Maybe you're setting e.Effect = DragDropEffects.All;
in this even and it will be fired repeatedly after you enter the target control instead of DragEnter
that will be fired just once.
你没有说你是否使用DragOver。也许你正在设置e.Effect = DragDropEffects.All;在这个偶数中,它将在您输入目标控件后重复触发,而不是只触发一次的DragEnter。
private void arbol_DragOver(object sender, DragEventArgs e)
{
if (some_reason)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.All;
}
If you didn't use this event or didn't modify e.Effect
within, then it's hard to say. Code is needed.
如果您没有使用此事件或没有修改内部的e.Effect,那么很难说。代码是必需的。