C# 如何确定DragSource中的Drop事件
我有一个列表框,其中包含一些文本值C# 如何确定DragSource中的Drop事件,c#,wpf,drag-and-drop,C#,Wpf,Drag And Drop,我有一个列表框,其中包含一些文本值 <ListBox x:Name="DragSource" PreviewMouseMove="DragSource_OnPreviewMouseMove" SelectedValuePath="Content"> <ListBoxItem>first</ListBoxItem> <ListBoxItem>second</ListBoxItem> </ListBox> 可以将项目
<ListBox x:Name="DragSource" PreviewMouseMove="DragSource_OnPreviewMouseMove" SelectedValuePath="Content">
<ListBoxItem>first</ListBoxItem>
<ListBoxItem>second</ListBoxItem>
</ListBox>
可以将项目拖放到“我的其他列表框”或其他应用程序(如Word或Excel)中。如果DragDrop效果为“移动”,我如何检测文本被删除(例如Word)并删除ListBoxItem?没有第三方应用程序会告诉您它移动了ListBoxItem。充其量它将使用文本表示并告诉您它是复制的。获得移动需要一个drop目标,该目标可以在其DragEnter事件处理程序中识别您的对象,并确定它可以对此负责。只有您才能编写这样的事件处理程序。我的dragsource提供了反馈和QueryContinueDrag事件,在这里我可以找到现在应用的效果。QueryContinueDragEventArgs具有可以取消、删除或继续的操作。但我只收到Continue event.QCD,它只允许您在键盘或鼠标状态更改时更改D+D。是,操作设置为继续,因为这是默认设置。赋值结束D+D。
private void DragSource_OnPreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && DragSource.SelectedItem != null)
{
var data = new DataObject(DataFormats.Serializable, DragSource.SelectedItem);
var value = (string)DragSource.SelectedValue;
data.SetData(DataFormats.Text, value);
var de = DragDrop.DoDragDrop(DragSource, data, DragDropEffects.All);
}
}