1

所以我有一个objectlistview(实际上是一个treelistview)。我希望能够将一个项目从这里拖到一个富文本框中,并让它插入被拖项目的属性(在这种情况下Default_Heirarchy_ID

TreeListView 的对象模型是一个List<T>名为SpecItem.

这是我到目前为止所拥有的:

    public frmAutospecEditor(SpecItem siThis_, List<SpecItem> lstStock_)
    {
        InitializeComponent();

        txtFormula.DragEnter += new DragEventHandler(txtFormula_DragEnter);
        txtFormula.DragDrop += new DragEventHandler(txtFormula_DragDrop);
        ...
    }

    void txtFormula_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void tlvSpecItem_ItemDrag(object sender, ItemDragEventArgs e)
    {
        int intID = ((SpecItem)tlvSpecItem.GetItem(tlvSpecItem.SelectedIndex).RowObject).Default_Heirarchy_ID ??0;
        DoDragDrop(intID, DragDropEffects.Copy);
    }
    private void txtFormula_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {

        object objID = e.Data.GetData(typeof(String)); 
        //this is where it goes wrong - no matter what I try to do with this, it 
        //always returns either null, or the text displayed for that item in the TreeListView,               
        //NOT the ID as I want it to.
        string strID = (string)objID;
        txtFormula.Text = strID;
    }

我哪里错了?

干杯

4

1 回答 1

1

Drag 是您要从中获取数据的控件(您的 OLV)。Drop 是目标控件(您的文本框)。所以:

IsSimpleDragSourceOLV 的属性设置为 true。

在文本框中将AllowDrop属性设置为 true。然后处理DragEnter文本框的事件并使用DragEventArgs参数。

处理 ModelDropped 事件:

private void yourOlv_ModelDropped(object sender, ModelDropEventArgs e) 
{ 
   // If they didn't drop on anything, then don't do anything 
   if (e.TargetModel == null) return; 

   // Use the dropped data: 
   // ((SpecItem)e.TargetModel) 
   // foreach (SpecItem si in e.SourceModels) ...

   // e.RefreshObjects(); 
}

阅读更多: http: //objectlistview.sourceforge.net/cs/dragdrop.html#ixzz1lEt7LoGr

于 2012-02-02T15:39:16.320 回答