2013-03-08 164 views
0

我試圖通過掛接到DataGrid的SelectedCellsChanged事件來讓用戶避免在多列上進行單元選擇。WPF DataGrid:禁用多列選擇

不過,出於某種原因,我的代碼表現得有點奇怪。 This is what happens

這裏是我的代碼:

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
{ 
    foreach (DataGridCellInfo cell in e.AddedCells) 
    { 
     // If a selected cell is within a different column than the first selected cell, undo the selection (to prevent selections from crossing multiple columns) 
     if (cell.Column != e.AddedCells[0].Column) 
        this.chartDataGrid.SelectedCells.Remove(cell); 
    } 
} 

誰能告訴我什麼,我在這裏失蹤?

回答

0

好吧,我擺脫了這種行爲,但偶然發現了用新的代碼另一個問題,它看起來像這樣:

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
{ 
    foreach (DataGridCellInfo cell in e.AddedCells) 
    { 
     // If a selected cell is within a different column than the first selected cell, undo the selection (to prevent selections from crossing multiple columns) 
     if (cell.Column != this.chartDataGrid.SelectedCell[0].Column) 
      this.chartDataGrid.SelectedCells.Remove(cell); 
    } 
} 

現在我不能擴大我選擇到右邊的列(其中是好的),但我仍然可以在左邊。