'宣言 Public Property SelectionBehavior As SelectionBehavior
public SelectionBehavior SelectionBehavior {get; set;}
デフォルトで、UltraTree コントロールは異なる Nodes コレクションに属するノードの同時選択を許可しません。SelectionBehavior プロパティによって、ノードの UltraTreeNode.Level または UltraTreeNode.Parent にかかわらず、エンドユーザーがノード選択を拡張して元々選択されていたノードと異なる Nodes コレクションに属しているノードを含めることが可能です。
注: その他のプロパティ設定にかかわらず、Enabled プロパティが False に設定されているノードを選択できません。ノードの祖先のいずれかに対して Enabled が False に設定される時にこれも当てはまります。
コントロールの以前のバージョンでは、ノード選択の動作は SelectionType プロパティによって排他的に制御されました。特定のノードが選択可能だったかどうかは、プロパティの値は以下のように解決した上で (優先度の低い順に) SelectionType プロパティの解決された値によって決定されました:
SelectionBehavior プロパティが 'ExtendedAcrossCollections' または 'ExtendedAutoDragAcrossCollections' に設定されると、以下の状況を除いて Override の SelectionType プロパティはもはや適用できません:
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 ); } }