2013-10-21 94 views
0

所以我真的不擅長這個xaml的事情,我試圖到處尋找,但找不到任何有用的東西,希望有人能夠幫助我在這裏。獲取內容專注於WPF DataGrid

所以我有一個TemplateColumns的數據網格,我在這裏有一些控件,如TextBox的和ComboBox的。什麼即時嘗試完成這裏是當我從一個控件選項卡我想專注於同一行中的下一個控件,但現在發生的是該列獲取焦點,只有在那之後,當我再次按Tab選項控件將焦點,用更少的話來說,我不得不兩次從一個控件跳到另一個控件。我的DataGrid是這樣的:

   <DataGridTemplateColumn Header="Omschrijving" Width="150"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Grid> 
           <TextBox TabIndex="0" Name="txtOms" Text="{Binding txtOmschrijving}" Width="140" Height="24" /> 
          </Grid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

回答

0

既然沒有人能給出提示或幫助我有點挖倒在internetzz並找到了解決方法,希望能幫助別人需要:

Private Sub dgKasStaatRegels_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles dgKasStaatRegels.Loaded 
    Try 
     Dim CellFocusedChangedHandler As New RoutedEventHandler(AddressOf FocusChangedHandler) 
     [AddHandler](DataGridCell.GotKeyboardFocusEvent, CellFocusedChangedHandler) 
    Catch ex As Exception 
     WriteErrorLog("ucKasStaat", "dgKasStaatRegels_Loaded", ex) 
    End Try 
End Sub 
Private Sub FocusChangedHandler(sender As Object, args As RoutedEventArgs) 
    If args.RoutedEvent.RoutingStrategy = RoutingStrategy.Bubble Then 
     Dim DataGridCellObj As FrameworkElement = TryCast(args.OriginalSource, FrameworkElement) 
     If Keyboard.IsKeyDown(Key.Tab) Then 
      If DataGridCellObj IsNot Nothing Then 
       Dim txtb As TextBox = TryCast(DataGridCellObj, TextBox) 
       If txtb IsNot Nothing Then txtb.Focus() 

       Dim cb As ComboBox = TryCast(DataGridCellObj, ComboBox) 
       If cb IsNot Nothing Then 
        cb.Focus() 
        cb.IsDropDownOpen = True 
       End If 
      End If 
     End If 
    End If 
End Sub 
Public Shared Function FindParent(Of T As DependencyObject)(dependencyObject As DependencyObject) As T 
    Dim parent = VisualTreeHelper.GetParent(dependencyObject) 

    If parent Is Nothing Then 
     Return Nothing 
    End If 

    Dim parentT = TryCast(parent, T) 
    Return If(parentT, FindParent(Of T)(parent)) 
End Function 

快速解釋:在datagrid負載上添加一個CellFocusedChangedHandler處理程序,並在該子文件中追蹤該行內的對象是否爲文本框(在我的情況下)並設置它的焦點!

它爲我工作!