获取内容关注WPF DataGrid

时间:2013-10-21 12:58:14

标签: wpf datagrid focus

所以我真的不擅长这个xaml的东西,我试着到处寻找,但找不到任何有用的东西,希望有人能在这里帮助我。

所以我有一个带有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>

1 个答案:

答案 0 :(得分:0)

由于没有人可以提供暗示或帮助我有点挖掘网络并找到解决方案,希望能帮助其他有需要的人:

    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处理程序,并在该子句中跟踪该行内的对象是否为文本框(在我的例子中)并设置其焦点!

它对我有用!