2014-02-06 37 views
0

我正在努力從聚焦文本框存在於DataGrid中探頭。 真正的場景爲:專注於DataGrid中存在的文本框

我有一個數據網格中,我有數據網格的3 columns.The第三列包含2個不同的控制: - 一個是組合框,而另一個是文本框(兩者都是在相同的RowDefinition和ColumnDefinition)。 假設我有3行,所以想要的是當我在文本框中輸入一個文本(出現在第一行),然後按ENTER然後焦點應該移動到下一行TEXTBOX,但是現在發生的是它將整個單元格(其中包含ComboBox和TextBox)的DataGrid。

我試過幾個例子

private void dg_PreviewKeyDown(object sender, KeyEventArgs e) 
     { 
       var uie = e.OriginalSource as UIElement; 

       if (e.Key == Key.Enter) 
       { 
        e.Handled = true; 
        uie.MoveFocus(
        new TraversalRequest(FocusNavigationDirection.Next)); 


       } 

      } 

,但它的下一個單元格是一樣既ComoBox和TextBox聚焦小區中移動焦點。

我跟着這個鏈接太多,但它幫助力:

http://madprops.org/blog/enter-to-tab-as-an-attached-property/

幫我出Guys..Focusing控制總是我的錯誤。

+0

看看http://stackoverflow.com/questions/746069/wpf-datagrid-datagridtemplatecolumn-tab-focus-issue – Schuere

+0

答案被標記是沒有幫助的..Sorry – Vishal

回答

0

我終於找到了解決的辦法:

這裏是一個幫助我將焦點移到上輸入鍵碼:

注:UCItems是它綁定到DataGrid收集

private void dg_PreviewKeyDown(object sender, KeyEventArgs e) 
     { 

       if (e.Key == Key.Enter) 
       { 
        DataGridRow rowContainer = (DataGridRow)BomPickerGrid.ItemContainerGenerator.ContainerFromItem(UCItems[gridIndex+1]); 
        if (rowContainer != null) 
        { 
         DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer); 
         int columnIndex = BomPickerGrid.Columns.IndexOf(BomPickerGrid.CurrentColumn); 
         DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
         TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Last); 
    /* FocusNavigationDirection.Last is used because the 
TextBox I want to focus on is the Last control in that Cell*/ 
           request.Wrapped = true; 
           cell.MoveFocus(request); 
           rowContainer = (DataGridRow)BomPickerGrid.ItemContainerGenerator.ContainerFromItem(BomPickerGrid.CurrentItem); 
           BomPickerGrid.SelectedItem = BomPickerGrid.CurrentItem; 
           e.Handled = true; 
           BomPickerGrid.UpdateLayout(); 
          } 
         }