开发者

Wpf dragdrop, how to visually reject the drop during the drag?

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?

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.


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?

This is the sequence of events during a drag & drop in WPF (taken from MSDN) which might help work out what's going on...

  1. Dragging is initiated by calling the DoDragDrop method for the source control.

    The DoDragDrop method takes two parameters: * data, specifying the data to pass * allowedEffects, specifying which operations (copying and/or moving) are allowed

    A new DataObject object is automatically created.

  2. 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.
  3. 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.
  4. 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.
  5. 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.


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".


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.

    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.


If you want the app to take your change in account, you should set Handled property to true:

private void OnDragOver(object sender, DragEventArgs e)
{
    if (your_test) 
        e.Effects = DragDropEffects.Link;//or other effect you want
    else
       e.Effects = DragDropEffects.None;

    e.Handled = true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜