2012-04-23 18 views
5

में टैब के रूप में एंटर कुंजी का उपयोग करना मेरे पास WPF में DataGrid है।WPF DataGrid

मैं NextCell पर जाने के लिए जब मैं दर्ज करें और जब LastColumn तक पहुँच जाता है यह डिफ़ॉल्ट होना चाहिए बनाने या अगली पंक्ति में जाने की समारोह दर्ज मारा चाहते हैं।

मैं टैब

उपयोग करने के लिए मैं WPF में यह कैसे कर सकते चाहते हैं न।

+0

आपने क्या प्रयास किया है? ऐसा लगता है कि 'कुंजीअप' ईवेंट से अटैच करना बहुत आसान होगा और '' दबाएं दबाएं ... – Tejs

+0

लेकिन यह –

+0

dgrow.MoveFocus (नया TraversalRequest (FocusNavigationDirection.Next) काम नहीं कर रहा है; –

उत्तर

0

इसे आजमाएं, मुझे लगता है कि यह कम से कम मेरे लिए काम करता है।

//datagrid gotfocus event 
private void dataGrid1_GotFocus(object sender, RoutedEventArgs e) 
{ 
    DependencyObject dep = (DependencyObject)e.OriginalSource; 
    //here we just find the cell got focused ... 
    //then we can use the cell key down or key up 
    // iteratively traverse the visual tree 
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep == null) 
     return; 

    if (dep is DataGridCell) 
    { 
     DataGridCell cell = dep as DataGridCell; 
     //raise key down event of cell 
     cell.IsSelected = true; 
     cell.KeyDown += new KeyEventHandler(cell_KeyDown); 
    } 
} 

void cell_KeyDown(object sender, KeyEventArgs e) 
{ 
    DataGridCell cell = sender as DataGridCell; 
    if (e.Key == Key.Enter) 
    { 
     cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
     cell.IsSelected = false; 
     e.Handled = true; 
     cell.KeyDown -= cell_KeyDown; 
    } 
} 

इस कोड में जब एक सेल फोकस हो गया और उस अगले सेल पर उपयोगकर्ता कुंजी फोकस हो जाएगी। शुभकामनाएं उम्मीद है कि यह आपकी मदद करेगी।

संपादित करें:

सेट datagrid PreviewKeyDown घटना के रूप में यह कार्य करते हैं।

private void maindg_PreviewKeyDown(object sender, KeyEventArgs e) 
     { 

      //just accept enter key 
      if (e.Key != Key.Enter) return; 

     DependencyObject dep = (DependencyObject)e.OriginalSource; 
     //here we just find the cell got focused ... 
     //then we can use the cell key down or key up 
     // iteratively traverse the visual tree 
     while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     if (dep == null) 
      return; 

     if (dep is DataGridCell) 
     { 
      //cancel if datagrid in edit mode 
      maindg.CancelEdit(); 
      //get current cell 
      DataGridCell cell = dep as DataGridCell; 
      //deselect current cell 
      cell.IsSelected = false; 
      //find next right cell 
      var nextCell = cell.PredictFocus(FocusNavigationDirection.Right); 
      //if next right cell null go for find next ro first cell 
      if (nextCell == null) 
      { 
       DependencyObject nextRowCell; 
       nextRowCell = cell.PredictFocus(FocusNavigationDirection.Down); 
       //if next row is null so we have no more row Return; 
       if (nextRowCell == null) return; 
       //we do this because we cant use FocusNavigationDirection.Next for function PredictFocus 
       //so we have to find it this way 
       while ((nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left) != null) 
        nextRowCell = (nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left); 
       //set new cell as next cell 
       nextCell = nextRowCell; 
      } 
      //change current cell 
      maindg.CurrentCell = new DataGridCellInfo(nextCell as DataGridCell); 
      //change selected cell 
      (nextCell as DataGridCell).IsSelected = true; 
      // start edit mode 
      maindg.BeginEdit(); 
     } 
     //handl the default action of keydown 
     e.Handled = true; 
    } 
+0

पर काम नहीं कर रहा है, यह काम नहीं कर रहा है –

+0

@ ममद आर, यह काम कर रहा है, मुझे एक आवश्यकता है जब अगला सेल फोकस कर सके कि इसे संपादन योग्य कैसे बनाया जाए – Mussammil

+0

@Mussammil मेरा अंतिम संपादन पढ़ें। आशा है कि आप मदद करें :) –

-1

एक बहुत ही सरल कार्यान्वयन। विचार कुंजीडाउन ईवेंट को कैप्चर करना है और यदि कुंजी "एंटर" है, तो अगले टैब पर जाएं जो ग्रिड का अगला सेल है।

/// <summary> 
/// On Enter Key, it tabs to into next cell. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    var uiElement = e.OriginalSource as UIElement; 
    if (e.Key == Key.Enter && uiElement != null) 
    { 
     e.Handled = true; 
     uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
    } 
} 
संबंधित मुद्दे