2012-11-19 41 views

回答

3
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid) 
{ 
    var itemsSource = grid.ItemsSource as IEnumerable; 
    if (null == itemsSource) yield return null; 
    foreach (var item in itemsSource) 
    { 
     var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow; 
       if (null != row) yield return row; 
     } 
} 

//假設網格綁定到一些信息類

foreach (DataGridRow rowContainer in GetDataGridRows(gridname)) 
{ 
    if (rowContainer != null) 
    { 
    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 

    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
    if (cell == null) 
    { 
     dataGrid1.ScrollIntoView(rowContainer, dataGrid1.Columns[column]); 
     cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
     } 

     //start work with cell 
    } 
} 
+0

如何訪問每一個電池foreach循環內該行的觀察集合? – Kishor

+0

@WPFK - 如果你在foreach循環中用一些對象可觀察的集合bidnided你的網格,你閱讀與網格的每一行相關的對象,我在我的答案中給出了他的「Info info =(sInfo)r.Item;」 –

+1

但問題在這裏我想訪問單元格屬性,單元格的列標題等我無法達到綁定到數據網格的對象。 – Kishor