0

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

所以我有一个带有 TemplateColumns 的数据网格,其中我有一些控件,例如 TextBox 和 ComboBox。我在这里尝试完成的是,当我从一个控件中选择标签时,我想专注于同一行中的下一个控件,但现在发生的是列获得焦点,只有在那之后,当我再次按下标签时,控件才会成为焦点,简而言之,我必须按两次制表符才能从一个控件跳转到另一个控件。我的数据网格如下所示:

               <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>
4

1 回答 1

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

快速解释:在数据网格加载中添加一个 CellFocusedChangedHandler 处理程序,并在该子程序中跟踪该行内的对象是否是文本框(在我的情况下)并设置其焦点!

它对我有用!

于 2013-10-22T09:37:52.860 回答