2012-07-11 31 views
4

如何在擴展節點時讓TreeView更改寬度以便節點的標籤完全顯示。如何計算並更改樹形視圖寬度

首先,我設置DrawMode = OwnerDrawAll;

然後處理該事件DrawNode andin處理

e.DrawDefault = true; 
currentWith_ = Math.Max(currentWith_, e.Node.Bounds.Right); 

,然後在AfterExpand組與對照。但每次都不行。有時,with不會被更改或更改不正確。

如何解決此問題。 在此先感謝。

+0

你試過了什麼? – 2012-07-11 09:49:22

+0

wpf?的WinForms?添加標籤請參考 – 2012-07-11 09:52:58

回答

5

試試這個,這個作品成功:

private void treeViewAfterExpand(object sender, TreeViewEventArgs e) 
{ 
    int maxRight = treeView.ClientSize.Width; 

    if(e.Node.Nodes != null) 
     foreach (TreeNode node in e.Node.Nodes) 
     { 
      maxRight = Math.Max(maxRight, node.Bounds.Right); 
     } 

    treeView.ClientSize = new Size(maxRight, treeView.ClientSize.Height); 
} 
+0

'treeView.Bounds'是'treeView.ClientSize'添加的邊框等。 – 2012-07-11 10:25:12

+0

更新了答案! – Ria 2012-07-11 10:26:25

+0

不要工作!只有集合中的第一個節點具有Bounds屬性才能設置,另一個值設置爲0. – 2012-07-11 11:20:41

1

由利雅給出解決方案的工作,但在構造函數中沒有擴大時。在加載事件中展開而不是構造函數使其工作。 (無法評論,因爲低於50分。)

+0

解決方案在'OnLoad'構造函數中也不起作用。有缺陷或問題嗎?有解決方法嗎? – hellboy 2016-03-10 09:55:26