问题描述
我有一个像 Canvas
一样工作的自定义控件。我正在尝试通过 DragDrop.DoDragDrop()
(在 MouseLeftButtonDown
事件处理程序中)启动“移动”操作。但是,我要求用户在按下鼠标时按下 CTRL 键,因为我有一个单独的用例,用户需要在不按下 CTRL 键的情况下左键单击。
我尝试只将 DragDropEffects.Move
传递给 DoDragDrop()
,但按住 CTRL 键会在视觉上显示一个十字(“不允许”)光标。使用 DragDropEffects.Move | DragDropEffects.copy
将显示一个看起来像两个操作组合的光标。
处理 GiveFeedback
事件也没有用 - 我需要设置自定义光标,或使用不包含移动光标的系统光标之一。
如何让它在拖放过程中显示移动光标?
解决方法
这个从资源加载自定义光标的代码对我有用。
private Cursor customCursor = null;
private void Element_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
DataObject data = new DataObject(DataFormats.Text,<data>);
DragDrop.DoDragDrop((DependencyObject)e.Source,data,DragDropEffects.Copy | DragDropEffects.Move);
}
private void Element_GiveFeedback(object sender,GiveFeedbackEventArgs e)
{
if (e.Effects == DragDropEffects.Copy || e.Effects == DragDropEffects.Move)
{
if (customCursor == null)
{
var rs = Application.GetResourceStream(new Uri("move.cur",UriKind.Relative));
customCursor = new Cursor(rs.Stream);
}
e.UseDefaultCursors = false;
Mouse.SetCursor(customCursor);
}
else
e.UseDefaultCursors = true;
e.Handled = true;
}
在拖放操作期间按住 Ctrl
键与复制项目有关,因此您需要允许 DragDropEffects.Copy。