我有一个名为 RackViewer 的控件,其中包含另一个名为 ItemViewer 的控件。RackViewer 控件被动态添加到另一个父控件。在父控件中,我希望创建一个拖放事件,这样当用户将项目拖动到 RackViewer 中的 ItemViewer 控件上时,该事件就会触发。目前我正在这样做:
private void RackView_DragEnter(object sender, DragEventArgs e)
{
RackViewer rackView = (RackViewer)sender;
if (rackView.IsMouseOnItemViewer(rackView.PointToClient(new Point(e.X, e.Y))))
{
//content goes here
}
}
private void RackView_DragDrop(object sender, DragEventArgs e)
{
RackViewer rackView = (RackViewer)sender;
if(rackView.IsMouseOnItemViewer(rackView.PointToClient(new Point(e.X, e.Y))))
{
//content goes here
}
}
private void listbox_MouseDown(object sender, MouseEventArgs e)
{
ListBox dragDropSender = (ListBox)sender;
int index = dragDropSender.IndexFromPoint(new Point(e.X, e.Y));
if (index >= 0)
{
dragDropSender.SelectedIndex = index;
dragDropSender.DoDragDrop(dragDropSender.SelectedItem, DragDropEffects.Move);
}
}
请注意 IsMouseOnItemViewer 方法。此方法包含在 rackViewer 类中,并在继续拖放事件序列之前确认用户正在将项目拖动到 ItemViewer 上。我这样做的原因是因为我无法从 RackViewer 的父控件访问 ItemViewer,因为它是层次结构中更高的 2 个控件。“IsMouseOnItemViewer”方法如下所示:
public ItemViewer View { get {return itemViewer; } }
/// <param name="mousePoint"> The coordinates of the mouse in client coordinates. </param>
/// <returns> True if the user is hovering over the ItemViewer control and false otherwise.</returns>
public bool IsMouseOnItemViewer(Point mousePoint)
{
if (View.Bounds.Contains(mousePoint))
return true;
else
return false;
}
但是,由于某种原因,此事件永远不会返回 true!无论我将鼠标拖动到 RackViewer 控件上的哪个位置,它似乎都不会检测到它位于 ItemViewer 上。我究竟做错了什么?