2012-04-02 32 views
10

我有一個DataGrid WPF控件,我想要獲取特定的DataGridCell。我知道行和列索引。我怎樣才能做到這一點?從DataGrid中選擇DataGridCell

我需要DataGridCell,因爲我必須有權訪問其內容。所以如果我有(例如)一列DataGridTextColum,我的內容將是一個TextBlock對象。

+0

你好,我實現了方法,您可以訪問所選單元格的所有屬性。 – 2015-12-02 00:58:40

回答

4

您可以使用類似下面的代碼來選擇單元格:

var dataGridCellInfo = new DataGridCellInfo(
    dataGrid.Items[rowNo], dataGrid.Columns[colNo]); 

dataGrid.SelectedCells.Clear(); 
dataGrid.SelectedCells.Add(dataGridCellInfo); 
dataGrid.CurrentCell = dataGridCellInfo; 

我看不到的方式來直接更新特定單元格的內容,所以爲了更新特定的內容電池我會做以下

// gets the data item bound to the row that contains the current cell 
// and casts to your data type. 
var item = dataGrid.CurrentItem as MyDataItem; 

if(item != null){ 
    // update the property on your item associated with column 'n' 
    item.MyProperty = "new value"; 
} 
// assuming your data item implements INotifyPropertyChanged the cell will be updated. 
+1

但行不是[DataGrid]的成員(http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.aspx) – gliderkite 2012-04-03 16:14:34

+0

你沒有錯。改變了答案,昨晚肯定已經太累了:-) – Phil 2012-04-03 16:51:10

+1

好吧,用這種方式工作(如果SelectionUnit不是Row),但我需要[DataGridCell](http://msdn.microsoft.com/en- us/library/system.windows.controls.datagridcell.aspx)而不是DataGridCellInfo,因爲我需要訪問DataGridCell內容。我試過[this](http://stackoverflow.com/questions/1764498/wpf-datagrid-programmatically-editing-a-cell),但GetVisualChild方法返回一個空的子元素,因此不起作用。 – gliderkite 2012-04-03 22:21:37

2

你可以簡單地使用這個擴展方法 -

public static DataGridRow GetSelectedRow(this DataGrid grid) 
{ 
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem); 
} 

你可以通過現有的行和列id來獲取一個DataGrid的細胞:

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column) 
{ 
    if (row != null) 
    { 
     DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); 

     if (presenter == null) 
     { 
      grid.ScrollIntoView(row, grid.Columns[column]); 
      presenter = GetVisualChild<DataGridCellsPresenter>(row); 
     } 

     DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
     return cell; 
    } 
    return null; 
} 

grid.ScrollIntoView是使這項工作的情況下,數據網格是虛擬化的關鍵和所需的細胞是不是針對當前。

檢查此鏈接的詳細信息 - Get WPF DataGrid row and cell

1

這裏是我使用的代碼:

/// <summary> 
    /// Get the cell of the datagrid. 
    /// </summary> 
    /// <param name="dataGrid">The data grid in question</param> 
    /// <param name="cellInfo">The cell information for a row of that datagrid</param> 
    /// <param name="cellIndex">The row index of the cell to find. </param> 
    /// <returns>The cell or null</returns> 
    private DataGridCell TryToFindGridCell(DataGrid dataGrid, DataGridCellInfo cellInfo, int cellIndex = -1) 
    { 
     DataGridRow row; 
     DataGridCell result = null; 

     if (dataGrid != null && cellInfo != null) 
     { 
      if (cellIndex < 0) 
      { 
       row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item); 
      } 
      else 
      { 
       row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(cellIndex); 
      } 

      if (row != null) 
      { 
       int columnIndex = dataGrid.Columns.IndexOf(cellInfo.Column); 

       if (columnIndex > -1) 
       { 
        DataGridCellsPresenter presenter = this.FindVisualChild<DataGridCellsPresenter>(row); 

        if (presenter != null) 
        { 
         result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell; 
        } 
        else 
        { 
         result = null; 
        } 
       } 
      } 
     } 

     return result; 
    }` 

這是假設的DataGrid已經被加載(執行自己的加載處理程序)。

+1

幫助我,菲爾描述的常見方式上面的答案沒有工作原因奇怪的原因,我在這篇文章中描述: http://stackoverflow.com/questions/19889780/wpf-datagrid-unable-to-select-cells-programmatically – 2013-11-10 12:58:32

+0

「FindVisualChild」方法實現? – NathanAldenSr 2016-06-24 13:08:49