2016-07-28 49 views
0

假設我們有一個帶自定義元素和空白麪板的ListBox。當你將這些項目拖到面板上時,它們必須建立在特定的邏輯上。例如,如果沒有任何面板,則該元素位於中間。但是,如果存在,那麼新元素應該保持在離它最近的元素附近。這樣就可以實現?如何創建一個支持拖放的面板?

例如:

+0

這聽起來更像是佈局問題,而不像拖曳式拖放。你能否更好地呈現它(帶截圖)? – Sinatr

+0

@Sinatr,例如https://gyazo.com/4d3ea832eb937abc1f3ce0f1e9979157 – Lightness

+0

例子是真不明白,但畫面看起來要區分其附近現有項目新項目的邊緣被放棄了。對?一種可能性是確定位置(在'Canvas'內搜索要求拖放的問題,例如[this](http://stackoverflow.com/q/21833168/1997232))並且位置也存儲在ViewModel項中,那麼無論何時在頂部或右側添加新項目,您都可以使用它們來確定。另一個是讓新項目也接受拖放(項目視圖應創建* hot-spot *區域接受拖放)。 – Sinatr

回答

0

我改性this answer工作阻力,並通過添加一些排序邏輯下降實現。將項目放在視覺中間可以使用CSS樣式完成。

假設:「最接近它」的意思是最接近字母。

public object lb_item = null; 

private void listBox1_DragLeave(object sender, EventArgs e) 
{ 
    ListBox lb = sender as ListBox; 

    lb_item = lb.SelectedItem; 
    lb.Items.Remove(lb.SelectedItem); 
} 

private void listBox1_DragEnter(object sender, DragEventArgs e) 
{  
    if (lb_item != null) 
    { 
     listBox1.Items.Add(lb_item); 
     lb_item = null; 

     // Here I added the logic: 
     // Sort all items added previously 
     // (thereby placing item in the middle). 
     listBox1.Sorted = true; 
    } 
} 


private void listBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    lb_item = null; 

    if (listBox1.Items.Count == 0) 
    { 
     return; 
    }     

    int index = listBox1.IndexFromPoint(e.X, e.Y); 
    string s = listBox1.Items[index].ToString(); 
    DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);  
} 

private void Form1_DragDrop(object sender, DragEventArgs e) 
{    
    lb_item = null; 
} 
+0

更新問題。 – Lightness

相關問題