2011-05-04 3 views
3

DataGrid의 마지막 셀에서 'Tab'키를 누르면 데이터 그리드에 새 줄을 추가하고 싶습니다.WPF dataGrid의 Tab 키에 새 줄을 추가하는 방법

MVVM 패턴을 사용하고 있습니다. InsertNewLineCommand에 다음 코드를

<DataGrid.InputBindings> 
     <KeyBinding Command="{Binding Path=InsertNewLineCommand}" Key="Tab"></KeyBinding> 
    </DataGrid.InputBindings> 

그리고 추가 :

private void ExecuteInsertNewLineCommand() 
    { 
     //Checked is SelectedCell[0] at last cell of the datagrid 
     { 
      InsertNewLine(); 
     } 
    } 

그러나 문제는 키 바인딩을 추가하는 것입니다 = '나는 솔루션과 함께, 나는이 데이터 그리드의 결합 입력에 Tab 키 만 담당 TAB '정상적인 탭 기능으로 인해 장애가 발생했습니다 (다음 셀로 이동 중 ...)

+0

, 왜 그냥 후 다른 추가하지, 그리고에 SelectedCell 이동 프로그래밍 방식으로 DataGrid의 다음 셀을? – Tyrsius

답변

1

마지막 열에 있는지 확인한 다음 명령을 실행하십시오.

PreviewKeyDown을 사용 중이므로 로직을 테스트 할 수 있지만 executeCommand 메소드에 넣을 수 있습니다. 어쨌든, 이것은 당신이 시작할 수 있어야합니다

<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown" SelectionUnit="Cell" .... 

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    if (!Keyboard.IsKeyDown(Key.Tab)) return; 
    var dataGrid = (DataGrid) sender; 

    var current = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn); 
    var last = dataGrid.Columns.Count - 1; 

    if (current == last) 
     ExecuteInsertNewLineCommand(); 

}

당신은 이미 SelectedCell이 마지막 있는지 확인을 한 것으로 나타났습니다
관련 문제