2016-07-28 27 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

相关问题