|
Как програмно найти, (чтоб выделить) узел дерева если он не корневой?
#37060294
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
|
+
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119.
public static class TreeViewExtensions
{
/// <summary>
/// Selects an item in a TreeView using a path
/// </summary>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="path">The path to the selected item.
/// Components of the path are separated with Path.DirectorySeparatorChar.
/// Items in the control are converted by calling the ToString method.</param>
public static void SetSelectedItem(this TreeView treeView, string path)
{
treeView.SetSelectedItem(path, item => item.ToString());
}
/// <summary>
/// Selects an item in a TreeView using a path and a custom conversion method
/// </summary>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="path">The path to the selected item.
/// Components of the path are separated with Path.DirectorySeparatorChar.</param>
/// <param name="convertMethod">A custom method that converts items in the control to their respective path component</param>
public static void SetSelectedItem(this TreeView treeView, string path,
Func<object, string> convertMethod)
{
treeView.SetSelectedItem(path, convertMethod, Path.DirectorySeparatorChar);
}
/// <summary>
/// Selects an item in a TreeView using a path and a custom path separator character.
/// </summary>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="path">The path to the selected item</param>
/// <param name="separatorChar">The character that separates path components</param>
public static void SetSelectedItem(this TreeView treeView, string path,
char separatorChar)
{
treeView.SetSelectedItem(path, item => item.ToString(), separatorChar);
}
/// <summary>
/// Selects an item in a TreeView using a path, a custom conversion method,
/// and a custom path separator character.
/// </summary>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="path">The path to the selected item</param>
/// <param name="convertMethod">A custom method that converts items in the control to their respective path component</param>
/// <param name="separatorChar">The character that separates path components</param>
public static void SetSelectedItem(this TreeView treeView, string path,
Func<object, string> convertMethod, char separatorChar)
{
treeView.SetSelectedItem<string>(
path.Split(new char[] { separatorChar },
StringSplitOptions.RemoveEmptyEntries),
(x, y) => x == y,
convertMethod
);
}
/// <summary>
/// Selects an item in a TreeView using a custom item chain
/// </summary>
/// <typeparam name="T">The type of the items present in the control and the chain</typeparam>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="items">The chain of items to walk. The last item in the chain will be selected</param>
public static void SetSelectedItem<T>(this TreeView treeView, IEnumerable<T> items)
where T : class
{
// Use a default compare method with the '==' operator
treeView.SetSelectedItem<T>(items,
(x, y) => x == y
);
}
/// <summary>
/// Selects an item in a TreeView using a custom item chain and item comparison method
/// </summary>
/// <typeparam name="T">The type of the items present in the control and the chain</typeparam>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="items">The chain of items to walk. The last item in the chain will be selected</param>
/// <param name="compareMethod">The method used to compare items in the control with items in the chain</param>
public static void SetSelectedItem<T>(this TreeView treeView, IEnumerable<T> items,
Func<T, T, bool> compareMethod)
{
treeView.SetSelectedItem<T>(items, compareMethod, null);
}
/// <summary>
/// Selects an item in a TreeView using a custom item chain, an item comparison method,
/// and an item conversion method.
/// </summary>
/// <typeparam name="T">The type of the items present in the control and the chain</typeparam>
/// <param name="treeView">The TreeView to select an item in</param>
/// <param name="items">The chain of items to walk. The last item in the chain will be selected</param>
/// <param name="compareMethod">The method used to compare items in the control with items in the chain</param>
/// <param name="convertMethod">The method used to convert items in the control to be compared with items in the chain</param>
public static void SetSelectedItem<T>(this TreeView treeView, IEnumerable<T> items,
Func<T, T, bool> compareMethod, Func<object, T> convertMethod)
{
// Setup default options for a TreeView
UIUtility.SetSelectedItem<T>(treeView,
new SetSelectedInfo<T>()
{
Items = items,
CompareMethod = compareMethod,
ConvertMethod = convertMethod,
OnSelected = delegate(ItemsControl container, SetSelectedInfo<T> info)
{
var treeItem = (TreeViewItem)container;
treeItem.IsSelected = true;
treeItem.BringIntoView();
},
OnNeedMoreItems = delegate(ItemsControl container, SetSelectedInfo<T> info)
{
((TreeViewItem)container).IsExpanded = true;
}
}
);
}
}
|
|
|