我正在試圖製作一個程序,允許將文件拖放到ListView
。我使用ListView.InsertionMark
來確定插入文件的位置。當我放棄文件時,InsertionMark
不會消失,因爲DragLeave
事件未觸發;如果我將拖放效果更改爲DragDropEffects.None
,則釋放鼠標時將觸發DragLeave
事件。這是爲什麼發生?DragDropEffects.Link拖放效果爲什麼DragLeave沒有被觸發?
此外,文檔中還提到「如果鍵盤或鼠標按鈕狀態有變化,則會引發QueryContinueDrag
事件並確定是繼續拖動,刪除數據還是取消基於該值的操作的Action
財產的事件的QueryContinueDragEventArgs
「。但QueryContinueDrag
事件也未觸發。
public partial class MainForm : Form
{
private ListView listView1 = new ListView();
public MainForm()
{
InitializeComponent();
this.Controls.Add(listView1);
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.Columns.Add("Test");
listView1.Items.Add("0");
listView1.AllowDrop = true;
listView1.DragEnter += listView1_DragEnter;
listView1.DragOver += listView1_DragOver;
listView1.DragLeave += listView1_DragLeave;
listView1.DragDrop += listView1_DragDrop;
listView1.GiveFeedback += listView1_GiveFeedback;
listView1.QueryContinueDrag += listView1_QueryContinueDrag;
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link | DragDropEffects.Scroll;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
// This is not done, only to show this problem.
listView1.InsertionMark.Index = 0;
listView1.InsertionMark.AppearsAfterItem = true;
}
private void listView1_DragLeave(object sender, EventArgs e)
{
listView1.InsertionMark.Index = -1;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
// listView1.InsertionMark.Index = -1; // Is this really necessary?
}
private void listView1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
this.Text = "listView1_QueryContinueDrag";
}
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
this.Text = "listView1_GiveFeedback";
}
}
我必須手動設置listView1.InsertionMark.Index = -1
隱藏InsertionMark
?無論如何觸發DragLeave
事件時,拖放效果是DragDropEffects.Link
?
但是,如果我不設置'e.Effect = DragDropEffects.Link | DragDropEffects.Scroll',當刪除文件時,'DragLeave'事件被觸發。 – EFanZh
@EFanZh - 如果你正在談論代碼的分支,你可以將效果設置爲None,那麼就不會發生拖放操作。如果釋放鼠標按鈕,這將有效地強制取消該操作。 –