Back to Devexpress

TreeList.CustomizeNewNodeFromOuterData Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-1eda369f.md

latest9.8 KB
Original Source

TreeList.CustomizeNewNodeFromOuterData Event

Allows you to initialize a new node when a user drops the node from another TreeList control onto the current TreeList control.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("DragDrop")]
public event CustomizeNewNodeFromOuterDataEventHandler CustomizeNewNodeFromOuterData
vb
<DXCategory("DragDrop")>
Public Event CustomizeNewNodeFromOuterData As CustomizeNewNodeFromOuterDataEventHandler

Event Data

The CustomizeNewNodeFromOuterData event's data class is CustomizeNewNodeFromOuterDataEventArgs. The following properties provide information specific to this event:

PropertyDescription
DestinationNodeGets the node onto which (or relative to which) the CustomizeNewNodeFromOuterDataEventArgs.SourceNode is dropped.
DestinationNodesGets the collection of sibling nodes relative to which the CustomizeNewNodeFromOuterDataEventArgs.SourceNode is dropped.
HandledGets or sets whether you perform all the required actions manually and no default processing is required.
NewDataProvides access to the dictionary that contains data to initialize a new node.
SourceNodeGets the node (from another TreeList control) being dragged.

Remarks

Enable Node Drag & Drop

Set the following settings (TreeList.OptionsDragAndDrop) to allow a user to move or copy/paste nodes between different TreeList controls (the user drags a node from the source TreeList and drops it onto the target TreeList):

  • Set the DragNodesMode property to Single or Multiple for both TreeList controls to allow users to drag and drop one or multiple nodes. Enable the MultiSelect option to enable multiple node selection.
  • Enable the AcceptOuterNodes option for the target (destination) TreeList control.
  • Enable the CanCloneNodesOnDrop option for the target TreeList control to allow a user to copy the node(s) from the source TreeList and insert the node(s) in the target TreeList. The user must hold down the CTRL or SHIFT key while dragging. If this option is disabled, the dragged node(s) will be removed from the source TreeList.
  • In bound mode, initialize the target TreeList’s KeyFieldName and ParentFieldName properties.

Important

Users cannot drag-and-drop the nodes between TreeList controls that are bound to the same data source.

Initialize a New Node

Handle the target TreeList’s CustomizeNewNodeFromOuterData event to initialize a new node. The TreeList raises this event when the user drops the node onto it. If the user drags multiple nodes, the TreeList raises the CustomizeNewNodeFromOuterData event for each node.

Use the e.NewData property to supply data (the key-value pairs) for a new node(s). The keys are strings that specify field names. If the source and target TreeList controls are bound to the same data field, a value in the key-value pair is set to a value from this data field. Otherwise, you need to initialize the key-value pairs manually.

If the e.Handled parameter is set to false, the target TreeList creates and initializes a node with data from the e.NewData dictionary. If the e.Handled parameter is set to true, the TreeList does not create the node. You should manually create and initialize the node.

Example

The following example demonstrates how to implement multiple node drag-and-drop between two TreeList controls:

csharp
using DevExpress.XtraTreeList;

public Form1() {
    InitializeComponent();
    // Initializes the source TreeList control.
    sourceTreeList.DataSource = Task.Init();
    sourceTreeList.KeyFieldName = "ID";
    sourceTreeList.ParentFieldName = "ParentID";
    sourceTreeList.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Multiple;
    sourceTreeList.OptionsSelection.MultiSelect = true;

    // Initializes the target (destination) TreeList control.
    targetTreeList.DataSource = PlannedTask.Init();
    targetTreeList.KeyFieldName = "ID";
    targetTreeList.ParentFieldName = "ParentID";
    targetTreeList.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Multiple;
    targetTreeList.OptionsDragAndDrop.AcceptOuterNodes = true;
}
private void targetTreeList_CustomizeNewNodeFromOuterData(object sender, CustomizeNewNodeFromOuterDataEventArgs e) {
    e.NewData["TaskName"] = e.SourceNode["Name"];
    e.NewData["CreateDate"] = DateTime.Now;
}

// Data objects.
public class PlannedTask {
    public int ID { get; set; }
    public int ParentID { get; set; }
    public string TaskName { get; set; }
    public DateTime CreateDate { get; set; } = DateTime.Now;
    public bool Finished { get; set; } = false;
    static public List<PlannedTask> Init() {
        return new List<PlannedTask>() {
            new PlannedTask(){ ID = 0, ParentID = 0, TaskName = "Planned Task A" }
        };
    }
}
public class Task {
    public int ID { get; set; }
    public int ParentID { get; set; }
    public string Name { get; set; }
    static public List<Task> Init() {
        return new List<Task>() {
            new Task(){ID = 0, ParentID = 0, Name = "Task 1" },
            new Task(){ID = 1, ParentID = 0, Name = "Task 2" },
            new Task(){ID = 2, ParentID = 1, Name = "Task 3" },
            new Task(){ID = 3, ParentID = 1, Name = "Task 4" },
            new Task(){ID = 4, ParentID = 1, Name = "Task 5" },
        };
    }
}
vb
Imports DevExpress.XtraTreeList

Public Sub New()
    InitializeComponent()
    ' Initializes the source TreeList control.
    sourceTreeList.DataSource = Task.Init()
    sourceTreeList.KeyFieldName = "ID"
    sourceTreeList.ParentFieldName = "ParentID"
    sourceTreeList.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Multiple
    sourceTreeList.OptionsSelection.MultiSelect = True

    ' Initializes the target (destination) TreeList control.
    targetTreeList.DataSource = PlannedTask.Init()
    targetTreeList.KeyFieldName = "ID"
    targetTreeList.ParentFieldName = "ParentID"
    targetTreeList.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Multiple
    targetTreeList.OptionsDragAndDrop.AcceptOuterNodes = True
End Sub
Private Sub targetTreeList_CustomizeNewNodeFromOuterData(ByVal sender As Object, ByVal e As CustomizeNewNodeFromOuterDataEventArgs)
    e.NewData("TaskName") = e.SourceNode("Name")
    e.NewData("CreateDate") = Date.Now
End Sub

' Data objects.
Public Class PlannedTask
    Public Property ID() As Integer
    Public Property ParentID() As Integer
    Public Property TaskName() As String
    Public Property CreateDate() As Date
    = Date.Now
    public Boolean Finished {get;set;}
    = False
    public static List(Of PlannedTask) Init()
        Return New List(Of PlannedTask)() From {
            New PlannedTask() With {.ID = 0, .ParentID = 0, .TaskName = "Planned Task A"}
        }
End Class
public class Task
    public Integer ID {get;set;}
    public Integer ParentID {get;set;}
    public String Name {get;set;}
    public static List(Of Task) Init()
        Return New List(Of Task)() From {
            New Task() With {.ID = 0, .ParentID = 0, .Name = "Task 1"},
            New Task() With {.ID = 1, .ParentID = 0, .Name = "Task 2"},
            New Task() With {.ID = 2, .ParentID = 1, .Name = "Task 3"},
            New Task() With {.ID = 3, .ParentID = 1, .Name = "Task 4"},
            New Task() With {.ID = 4, .ParentID = 1, .Name = "Task 5"}
        }

See Also

Drag-and-Drop Tree List Nodes

Drag-and-Drop Behavior

How to: Drag XtraGrid rows to the XtraTreeList

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace