2016-10-07 7 views
0

데이터 격자에 DataGridComboBoxColumn이있는 DataGrid가 있습니다.WPF DataGridComboBoxColumn 키 편집 편집

사용자가 입력하는 것으로 편집 모드로 들어갈 수 있기를 바랍니다.
DataGridTextColumn의 기본 동작이며이 열 유형에 대해 편집을 사용하려면 F2 키를 눌러야한다는 점이 마음에 들지 않습니다.

F2를 누르지 않아도 DataGridComboBoxColumn 편집 모드로 들어가게하려면 어떻게해야합니까? Key Press에서 이상적이지만, 초점이 맞으면 편집 모드로 전환해도 괜찮습니다. DataGrid를의 기본 기능을 다시 가져 허용 대답에

솔루션 수정 :

void Cell_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     if(e.Key == Key.Enter || e.Key == Key.Tab) 
     { 
      dgBins.CommitEdit(); 
      dgBins.SelectedIndex += 1; 
     }else if(e.Key.ToString().Length == 1 
      || (e.Key.ToString().StartsWith("D") && e.Key.ToString().Length == 2) 
      || e.Key.ToString().StartsWith("NumPad") 
      || e.Key == Key.Delete 
      || e.Key == Key.Back) 
     { 
      if (e.OriginalSource is DataGridCell) 
      { 
       DataGridCell cell = (sender as DataGridCell); 
       Control elem = FindChild<Control>(cell, null); 
       elem.Focus(); 
      } 
     } 
    } 

답변

1
<DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <EventSetter Event="PreviewKeyDown" Handler="Cell_PreviewKeyDown"/> 
      <EventSetter Event="GotFocus" Handler="Cell_GotFocus"/> 
     </Style> 
    </DataGrid.CellStyle> 

핸들러 :

void Cell_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.OriginalSource is DataGridCell) 
    { 
     DataGridCell cell = (sender as DataGridCell); 
     Control elem = FindChild<Control>(cell, null); 
     elem.Focus(); 
    } 
} 

void Cell_GotFocus(object sender, RoutedEventArgs e) 
{ 
    DataGridCell cell = (sender as DataGridCell); 
    cell.IsEditing = true; 
} 

도우미 :

public static T FindChild<T>(DependencyObject parent, string childName) 
     where T : DependencyObject 
{ 
    // Confirm parent and childName are valid. 
    if (parent == null) return null; 

    T foundChild = null; 

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < childrenCount; i++) 
    { 
     var child = VisualTreeHelper.GetChild(parent, i); 
     // If the child is not of the request child type child 
     T childType = child as T; 
     if (childType == null) 
     { 
      // recursively drill down the tree 
      foundChild = FindChild<T>(child, childName); 

      // If the child is found, break so we do not overwrite the found child. 
      if (foundChild != null) break; 
     } 
     else if (!string.IsNullOrEmpty(childName)) 
     { 
      var frameworkElement = child as FrameworkElement; 
      // If the child's name is set for search 
      if (frameworkElement != null && frameworkElement.Name == childName) 
      { 
       // if the child's name is of the request name 
       foundChild = (T)child; 
       break; 
      } 
     } 
     else 
     { 
      // child element found. 
      foundChild = (T)child; 
      break; 
     } 
    } 

    return foundChild; 
} 

이 방법으로 문제가 해결 될 경우.

+0

거의 작동합니다. 입력, 탈출 및 탭 키를 무시하도록 수정해야했습니다. 또한 이제 사용자는 enter를 두 번 눌러 다음 행으로 이동해야합니다. 중지 방법을 찾으려고 시도합니다. – MrZander

+0

@MrZander 그것은 별도의 질문에 대한 후보입니다. 그래서 u에게 별도로 요청하십시오. 그리고이 대답이 현재의 질문에 적절하게 표시되어 있다면. – AnjumSKhan

+0

물론 이것은 이것이 편집 모드로 들어가게하지만, 데이터 그릿의 다른 모든 기능을 거의 손상시킵니다. 이상적이지 않습니다. 여하튼 문제를 수정 한 수정 사항으로 질문을 업데이트했습니다. – MrZander

1

당신은 SelectedCellsChanged 이벤트를 사용하려고 할 수 있습니다. 그것은 초점을 변화에 작동합니다.

다른 행에서 해당 동작을 원하지 않는 경우 SelectionUnit="Cell"DataGrid 인 경우 e.AddedCells[0].Column 속성을 확인할 수 있습니다.

private void dgTest_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
{ 
    (sender as DataGrid).BeginEdit(); 
}