我有一个树视图。我在 TreeViewItems 上设置了一个 ContextMenu。当我通过右键单击一个项目打开 ContextMenu 并选择另一个项目时(当 ContextMenu 打开时),我希望刚刚单击的项目被选中而不做任何事情。相反,框架认为我想拖动打开 ContextMenu 的项目,因此调用 Drop 处理程序。我怎么解决这个问题。谢谢
private void TreeViewPreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && !_isDragging)
{
var position = e.GetPosition(sender as IInputElement);
if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
{
StartDrag();
}
}
}
private void TreeViewPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(sender as IInputElement);
}
private void TemplateTreeViewDrop(object sender, DragEventArgs e)
{
if (_isDragging && (e.Source as TreeView) != null)
{
dragQuestion = e.Data.GetData(typeof(QuestionListItem)) as QuestionListItem;
dropQuestion = GetItemAtLocation(e.GetPosition(TemplateTreeView));
if (dragQuestion != null && dropQuestion != null && dragQuestion!=dropQuestion)
{
viewModel.MoveQuestion(dragQuestion, dropQuestion);
}
}
e.Handled = true;
dragQuestion = null;
}
private void StartDrag()
{
var temp = TemplateTreeView.SelectedItem as QuestionListItem;
if(temp == null) return;
_isDragging = true;
var data = new DataObject(temp);
DragDrop.DoDragDrop(TemplateTreeView, data, DragDropEffects.Move);
_isDragging = false;
}