-1
A
回答
0
當你說'選擇'時,你的意思是用複選框嗎?如果您只是指「突出顯示」,那麼這與樹形控件的設計不符 - 每次只能突出顯示一個分支/葉子(以顯示您當前選擇的分支)。如果您的意思是在父分支的框中進行檢查也會對所有孩子的方框進行檢查,那麼您需要響應點擊分支時觸發的事件,檢查選中的狀態並手動將分支的孩子移至設置他們的檢查狀態。
2
這有點像你想:
// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
// NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
// After a tree node's Checked property is changed, all its child nodes are updated to the same value.
private void node_AfterCheck(object sender, TreeViewEventArgs e)
{
// The code only executes if the user caused the checked state to change.
if(e.Action != TreeViewAction.Unknown)
{
if(e.Node.Nodes.Count > 0)
{
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
0
上面的代碼功能不正常 - 這是從MSDN AfterCheck事件主題然而事件未在雙擊可靠解僱複製/粘貼代碼 - 你需要混合它禁用雙擊的代碼 - 這是我在MSDN中找到的解決方法:
public class MyTreeView : TreeView
{
#region Constructors
public MyTreeView()
{
}
#endregion
#region Overrides
protected override void WndProc(ref Message m)
{
// Suppress WM_LBUTTONDBLCLK on checkbox
if (m.Msg == 0x0203 && CheckBoxes && IsOnCheckBox(m))
{
m.Result = IntPtr.Zero;
}
else
{
base.WndProc(ref m);
}
}
#endregion
#region Double-click check
private int GetXLParam(IntPtr lParam)
{
return lParam.ToInt32() & 0xffff;
}
private int GetYLParam(IntPtr lParam)
{
return lParam.ToInt32() >> 16;
}
private bool IsOnCheckBox(Message m)
{
int x = GetXLParam(m.LParam);
int y = GetYLParam(m.LParam);
TreeNode node = GetNodeAt(x, y);
if (node == null)
return false;
int iconWidth = ImageList == null ? 0 : ImageList.ImageSize.Width;
int right = node.Bounds.Left - 4 - iconWidth;
int left = right - CHECKBOX_WIDTH;
return left <= x && x <= right;
}
const int CHECKBOX_WIDTH = 12;
#endregion
相關問題
- 1. JS樹 - 選擇所有子節點時選擇父節點
- 2. 窗體樹形視圖中的父節點選擇和子節點選擇
- 3. 父節點/父節點/類選擇器
- 4. 樹視圖選擇的節點問題
- 5. 選擇節點時省略子節點
- 6. 樹視圖 - 選擇的節點樣式不出現選擇的節點
- 7. 如何從樹視圖當前選擇的節點在C#
- 8. 當前不可見時展開/選擇樹形視圖節點
- 9. 從節點選擇中選擇節點
- 10. 在jstree中選擇子節點時檢查所有父節點
- 11. Java樹節點選擇
- 12. CSS3選擇器:選擇特定節點的父節點?
- 13. php DomXPath - 如何從當前節點的父節點中選擇子節點?
- 14. jquery選擇父節點
- 15. GWT CellTree選擇父節點
- 16. 選擇從子子節點的樹節點
- 17. HtmlAgilityPack和選擇節點和子節點
- 18. 樹視圖節點重選
- 19. 選擇節點
- 20. 選擇節點
- 21. XSL:選擇當前節點
- 22. 嵌套集模型查詢選擇父節點內的節點
- 23. XSLT選擇子節點中存在值的父節點
- 24. 根據子節點中的值選擇父/祖先節點
- 25. jsTree - 不要選擇禁用父節點的子節點嗎?
- 26. XSLT選擇通過動態父節點的子節點
- 27. 基於父節點和子節點的CSS選擇器?
- 28. XPath:選擇具有屬性的子節點的父節點
- 29. 算法在樹中選擇的節點和他們的父母
- 30. Dynatree:選擇節點時自動選擇所有兄弟節點