2013-08-22 42 views
3

我正在使用相對於窗口大小水平拉伸的數據網格。C#Wpf Datagrid最後一列寬度=「*」摺疊其他列

因爲我想擺脫在右側生成額外的列,我設置所有的列大小爲「自動」,除了最後一列。我將最後一列的大小設置爲「*」。

最後我擺脫了在右側生成的額外列。

但是現在,當我填充信息的數據網格時,除最後一列以外的所有列大小都摺疊爲它們的頁眉寬度。

爲什麼列具有標題的寬度?我將它們的寬度設置爲「自動」,他們也應該尊重它們的單元大小。

注意:我不想爲列設置MinWidths,它們應該根據列和單元大小自動調整大小。

謝謝..

+1

帖子相關XAML –

回答

2

嘗試使用下面的代碼:

private void FitToContent() 
    { 
     int numCols ; 
     int i ; 

     numCols = dg.Columns.Count() ; 
     i = 0 ; 

     // where dg is your data grid's name... 
     foreach (DataGridColumn column in dg.Columns) 
     { 
      if(i < numCols - 1) 
      { 
       //if you want to size ur column as per the cell content 
       column.Width = new DataGridLength(1.0, DataGridLengthUnitType.SizeToCells); 
       //if you want to size ur column as per the column header 
       column.Width = new DataGridLength(1.0, DataGridLengthUnitType.SizeToHeader); 
       //if you want to size ur column as per both header and cell content 
       column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto); 
      } 
      else 
      { 
       column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star); 
      } 
      i++ ; 
     } 
    } 

請務必保持Horizo​​ntalScrollVisibility自動所有列

+0

現在,空列在右側返回。 ( – user2617750

+0

@ user2617750,但調整大小的工作,以符合預期? –

+0

是的,但我不想額外的自動生成列 – user2617750