Version

SelectionBehavior Property

Determines whether UltraTreeNode instances which belong to different Nodes collections can be concurrently selected.
Syntax
'Declaration
 
Public Property SelectionBehavior As SelectionBehavior
public SelectionBehavior SelectionBehavior {get; set;}
Remarks

By default, the UltraTree control does not permit the concurrent selection of nodes which belong to different Nodes collections. The SelectionBehavior property makes it possible to allow the end user to extend node selection to include nodes which belong to a different Nodes collection than the originally selected node, regardless of the node's UltraTreeNode.Level or UltraTreeNode.Parent.

Note: Nodes on which the Enabled property is set to false cannot be selected, regardless of any other property setting. This is also true when Enabled is set to false for any of the node's ancestors.

In previous versions of the control, node selection behavior was governed exclusively by the SelectionType property. Whether a given node was selectable was determined by the resolved value of the SelectionType property, with the property's value being resolved as follows (in order of descending precedence):

  • If a non-default value is set on the Override exposed by the Nodes collection of the node's UltraTreeNode.Parent (or Nodes collection for root-level nodes), that value is used.
  • If a non-default value is set on the Override returned by the NodeLevelOverrides collection corresponding to the node's UltraTreeNode.Level, that value is used.
  • If a non-default value is set on the control-level Override, that value is used.
  • In the absence of an explicit setting, the 'Single' setting is used.
Note that the value of the SelectionType property of the Override exposed by an individual UltraTreeNode is not applicable; this was the case with legacy versions of the control as well.

When the SelectionBehavior property is set to 'ExtendedAcrossCollections' or 'ExtendedAutoDragAcrossCollections', the SelectionType property of the Override is no longer applicable, except under the following circumstances:

  • If SelectionType is set to 'None' on the Override exposed by the Nodes collection of the node's UltraTreeNode.Parent (or Nodes collection for root-level nodes), that value is honored, and selection of that node is disallowed. When such nodes are part of a range selection, the operation is allowed, but these nodes are excluded from the selection.
  • As with the previously mentioned case, the setting of 'None' is also honored when set on the Override returned from the NodeLevelOverrides collection for that node's level.
All other values of the SelectionType property are not applicable when SelectionBehavior is set to 'ExtendedAcrossCollections' or 'ExtendedAutoDragAcrossCollections', regardless of the Override on which they are set.

Example
The following code sample demonstrates how to use the UltraTree's SelectionBehavior property to allow a selection to contain nodes from different collections, while restricting the selection to nodes that belong to the last level of the tree:

Imports System.Collections.Generic
Imports Infragistics.Win
Imports Infragistics.Win.Misc.UltraWinTree


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '  Set the SelectionBehavior property to 'ExtendedAcrossCollections'
        '  to allow nodes from different collections to be selected concurrently.
        Me.ultraTree1.SelectionBehavior = SelectionBehavior.ExtendedAcrossCollections

        '  Add nodes to the tree
        Me.PopulateTree(Me.ultraTree1)

        '  Determine the deepest level 
        Dim deepestLevel As Integer = Me.GetDeepestLevel(Me.ultraTree1)

        '  Only allow selection of leaf nodes
        Me.AllowSelectionOnLevel(Me.ultraTree1, deepestLevel)
    End Sub

    Private Sub PopulateTree(ByVal treeControl As UltraTree)
        Try

            treeControl.BeginUpdate()
            treeControl.Nodes.Clear()

            Dim i As Integer
            For i = 0 To 2

                Dim text As String = String.Format("Root {0}", i)
                Dim rootNode As UltraTreeNode = treeControl.Nodes.Add(Nothing, text)
                Me.PopulateNodes(rootNode.Nodes, 1)
            Next

        Finally
            treeControl.EndUpdate()
        End Try

        treeControl.ExpandAll()

    End Sub

    Private Sub PopulateNodes(ByVal nodesCollection As TreeNodesCollection, ByVal level As Integer)

        If (level > 3) Then Return

        Dim parentNode As UltraTreeNode = nodesCollection.ParentNode

        Dim i As Integer
        For i = 0 To 2

            Dim text As String = String.Format("{0}\Node {1}", parentNode.Text, i)
            Dim node As UltraTreeNode = nodesCollection.Add(Nothing, text)

            If (level < 3) Then
                Me.PopulateNodes(node.Nodes, level + 1)
            End If
        Next

    End Sub

    Public Sub AllowSelectionOnLevel(ByVal tree As UltraTree, ByVal level As Integer)

        Dim levels As New List(Of Integer)
        levels.Add(level)
        Me.AllowSelectionOnLevels(tree, levels)

    End Sub

    Public Sub AllowSelectionOnLevels(ByVal tree As UltraTree, ByVal levels As List(Of Integer))

        Dim deepestLevel As Integer = Me.GetDeepestLevel(tree)

        Dim i As Integer
        For i = 0 To deepestLevel
            tree.NodeLevelOverrides(i).ResetSelectionType()
            If (levels.Contains(i)) Then Continue For
            tree.NodeLevelOverrides(i).SelectionType = SelectType.None
        Next

    End Sub

    Public Function GetDeepestLevel(ByVal tree As UltraTree) As Integer

        Dim deepestLevel As Integer = 0
        Me.GetDeepestLevel(tree.Nodes, deepestLevel)
        Return deepestLevel

    End Function

    Private Sub GetDeepestLevel(ByVal nodes As TreeNodesCollection, ByRef deepestLevel As Integer)

        Dim i As Integer
        For i = 0 To nodes.Count - 1
            Dim node As UltraTreeNode = nodes(i)
            If (i = 0) Then deepestLevel = Math.Max(deepestLevel, node.Level)
            If (node.HasNodes) Then Me.GetDeepestLevel(node.Nodes, deepestLevel)
        Next

    End Sub
using System.Collections.Generic;
using Infragistics.Win;
using Infragistics.Win.Misc.UltraWinTree;

    private void Form1_Load(object sender, EventArgs e)
    {
        //  Set the SelectionBehavior property to 'ExtendedAcrossCollections'
        //  to allow nodes from different collections to be selected concurrently.
        this.ultraTree1.SelectionBehavior = SelectionBehavior.ExtendedAcrossCollections;

        //  Add nodes to the tree
        this.PopulateTree( this.ultraTree1 );

        //  Determine the deepest level 
        int deepestLevel = this.GetDeepestLevel( this.ultraTree1 );

        //  Only allow selection of leaf nodes
        this.AllowSelectionOnLevel( this.ultraTree1, deepestLevel);
    }

    private void PopulateTree( UltraTree treeControl )
    {
        try
        {
            treeControl.BeginUpdate();

            treeControl.Nodes.Clear();

            for ( int i = 0; i < 3; i ++ )
            {
                string text = string.Format( "Root {0}", i );
                UltraTreeNode rootNode = treeControl.Nodes.Add( null, text );
                this.PopulateNodes( rootNode.Nodes, 1 );
            }

        }
        finally
        {
            treeControl.EndUpdate();
        }

        this.ultraTree1.ExpandAll();
    }

    private void PopulateNodes( TreeNodesCollection nodesCollection, int level )
    {
        if ( level > 3 )
            return;

        UltraTreeNode parentNode = nodesCollection.ParentNode;

        for ( int i = 0; i < 3; i ++ )
        {
            string text = string.Format( @"{0}\Node {1}", parentNode.Text, i );
            UltraTreeNode node = nodesCollection.Add( null, text );
            
            if ( level < 3 )
                this.PopulateNodes( node.Nodes, level + 1 );
        }            
    }

    public void AllowSelectionOnLevel( UltraTree tree, int level )
    {
        List<int> levels = new List<int>(1);
        levels.Add( level );
        this.AllowSelectionOnLevels( tree, levels );
    }

    public void AllowSelectionOnLevels( UltraTree tree, List<int> levels )
    {
        int deepestLevel = this.GetDeepestLevel( tree );
        for ( int i = 0; i <= deepestLevel; i ++ )
        {
            tree.NodeLevelOverrides[i].ResetSelectionType();

            if ( levels.Contains(i) )
                continue;

            tree.NodeLevelOverrides[i].SelectionType = SelectType.None;
        }

    }

    public int GetDeepestLevel( UltraTree tree )
    {
        int deepestLevel = 0;
        this.GetDeepestLevel( tree.Nodes, ref deepestLevel );
        return deepestLevel;
    }

    private void GetDeepestLevel( TreeNodesCollection nodes, ref int deepestLevel )
    {
        for ( int i = 0; i < nodes.Count; i ++ )
        {
            UltraTreeNode node = nodes[i];

            if ( i == 0 )
                deepestLevel = Math.Max( deepestLevel, node.Level );

            if ( node.HasNodes )
                this.GetDeepestLevel( node.Nodes, ref deepestLevel );
        }
    }
Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also