2011-03-08 2 views
5

이 WPF DataGrid 데이터 템플릿이 있습니다 :WPF Datagrid에서 Enter 키를 누르면 DataGridCellsPanel.BringIndexIntoView의 ArgumentOutOfRangeException이 발생합니까?

<DataGrid 
    CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" 
    CanUserSortColumns="False" 
    SelectionMode="Single" SelectionUnit="FullRow" GridLinesVisibility="Horizontal" 
    IsEnabled="{Binding Enabled}" 
    ItemsSource="{Binding ValuesDataTable}" 
    CellEditEnding="DataGrid_CellEditEnding"/> 

여기에 이벤트 처리기가 :

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
{ 
    if (e.EditAction == DataGridEditAction.Commit) 
    { 
     var textBox = e.EditingElement as TextBox; 
     var dataGrid = (DataGrid)sender; 
     var viewModel = dataGrid.DataContext as IHasEditableCell; 
     viewModel.EditCell(e.Row.GetIndex(), e.Column.DisplayIndex, textBox.Text); 
     dataGrid.CancelEdit(); 
    } 
} 

이 키를 PropertyChanged 발생시키는보기 모델의 ValuesDataTable 속성에 PropertyChanged 이벤트 DataGrid은에 바인딩됩니다.

셀을 편집하고 클릭하면 정상적으로 작동합니다. 그러나 편집 할 때 셀을 편집하고 Enter 키를 누르면이 런타임 예외가 발생합니다.

System.ArgumentOutOfRangeException was unhandled 
    Message=Specified argument was out of the range of valid values. 
Parameter name: index 
    Source=PresentationFramework 
    ParamName=index 
    StackTrace: 
     at System.Windows.Controls.DataGridCellsPanel.BringIndexIntoView(Int32 index) 
     at System.Windows.Controls.Primitives.DataGridCellsPresenter.ScrollCellIntoView(Int32 index) 
     at System.Windows.Controls.DataGrid.ScrollCellIntoView(Object item, DataGridColumn column)... 

... 이상합니다. 이 문제를 어떻게 해결할 수 있을지 생각해?

답변

5

내 코드에서 myDataGrid.ScrollIntoView(object item)을 직접 호출 할 때 비슷한 문제가있었습니다. 나는 전에 myDataGrid.UpdateLayout()를 호출하여 그것을 고쳤습니다. 해당하는 경우 시도해보십시오.

0

나는이 문제가 정확히 발생하지만 왜 그런 일이 발생하는지 아직 모른다. 오히려 불만족스럽게도 필자는 enter 키를 잡아 수동으로 편집을 위임하여 작업을 마무리했습니다.

private void MyDataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Return) 
     { 
      e.Handled = true; 
      MyDataGrid.CommitEdit(); 
     } 
    } 
관련 문제