2014-09-27 57 views
0

正如書名我想獲得selectedRow的所有單元格在編輯模式下提及。將在編輯模式下一個DataGrid中選定行的所有細胞WPF

到目前爲止,我已經嘗試瞭如下代碼中提到獲得DataGrid的當前單元格和當前行。

private void DataGrid_Edit_Click(object sender, RoutedEventArgs e) 
{ 
    int colIndex = 0; 
    int rowIndex = 0; 

    DependencyObject dep = (DependencyObject)e.OriginalSource; 
    while (dep != null && !(dep is DataGridCell)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep == null) 
     return; 

    if (dep is DataGridCell) 
    { 

     colIndex = ((DataGridCell)dep).Column.DisplayIndex; 

     while (dep != null && !(dep is DataGridRow)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     DataGridRow row = (DataGridRow)dep; 
     rowIndex = FindRowIndex(row); 
    } 

    while (dep != null && !(dep is DataGrid)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep == null) 
     return; 

    DataGrid dg = (DataGrid)dep; 

    for (int column = 0; column < colIndex; column++) 
    { 
     dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column]); 
     dg.BeginEdit(); 
    } 

} 

上述代碼的工作原理就像迭代遍歷指定行的所有單元格一樣。但最後,最後一個單元格被放入編輯模式,因此我無法在編輯模式下獲取先前的單元格。

您能否提供爲我得到了selectedRow的所有細胞,而不是最後一個單元格做一樣的嗎?

+0

遍歷選定的行和設置'IsEditing'屬性的所有細胞TRUE;針對小區。 – 2014-09-28 09:49:28

+0

@RohitVats我錯過了。我不知道DataGridCell上有任何叫做IsEditing的屬性。相反,我設置DataGrid的當前單元格。謝謝。現在它工作正常。 – Vishal 2014-09-28 20:24:53

+0

偉大的我已經轉換了答案,以便它可以幫助其他人。 – 2014-09-28 20:31:00

回答

1

BeginEdit()將使當前單元格處於編輯模式,並且任何單元格的結束編輯模式已處於編輯模式。這就是爲什麼你看到最後一個單元總是處於編輯模式。

所以,作爲一種解決方法是你可以做的是遍歷所選行並設置IsEditing屬性爲true小區所有單元格。

+0

再次感謝。是的,這個答案可能會在未來幫助某人。 – Vishal 2014-09-28 20:32:24