2012-07-23 2 views
0

DataGrids를 사용하여 WPF 프로젝트에서 작업 중입니다. 사용자가 원하는만큼 행을 선택하거나 단일 셀만 선택할 수있게하려고합니다. 즉 셀 범위를 선택 해제 할 수 있습니다. 그러나 나는 그 일을 할 수 없었다.DataGrid에서만 복수 행 또는 단일 셀 선택

이것이 가능합니까?

나는 다음과 같은 코드를 시도 :

public MyDataGrid : DataGrid 
{ 
    public ExtendedDataGrid() 
    { 
     SelectionMode = DataGridSelectionMode.Extended; 
     SelectionUnit = DataGridSelectionUnit.CellOrRowHeader; 
     this.SelectedCellsChanged += new SelectedCellsChangedEventHandler(MyDataGrid_SelectedCellsChanged); 
    } 

    void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
    { 
     if (this.SelectedCells.Count > 1) 
     {     
      DataGridCellInfo currentCell = this.CurrentCell; 
      this.SelectedCells.Clear(); 
      this.CurrentCell = currentCell; 
     } 
    } 

을하지만이 코드 나 전체 행을 선택하는 것을 허용하지 않습니다.

그래서 원하는만큼 많은 행을 선택할 수 있지만 사용자가 셀 범위를 선택하지 못하도록하는 방법이 있습니까 ??

미리 감사드립니다.

답변

1

나는 내 문제를 해결 한 생각 :이 우아한 해결책은 아니지만 espected으로 다른 사람이 더 나은 솔루션이있는 경우 지금까지, 내가 감사하겠습니다 작품을 알고

private void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
    { 
     int columnsCount = this.Columns.Count; 
     int selectedCells = SelectedCells.Count; 
     int selectedItems = SelectedItems.Count; 

     if (selectedCells > 1) 
     { 
      if (selectedItems == 0 || selectedCells % selectedItems != 0) 
      { 
       DataGridCellInfo cellInfo = SelectedCells[0]; 
       SelectedCells.Clear(); 
       SelectedCells.Add(cellInfo); 
       CurrentCell = SelectedCells[0]; 
      } 
     } 
    }