WPF DataGrid,当按下向下箭头键时,TextBox不会获得焦点

时间:2012-05-23 09:37:24

标签: c# wpf datagrid textbox

我在文本框中放了一些文本框 我真正需要的是,当我按下向下箭头或向上箭头键时,下一行中的下一个文本框或控件应该聚焦

        <owpfc:FFDataGrid ItemsSource="{Binding Path=Ss}" AutoGenerateColumns="False" SelectionUnit="FullRow">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewKeyDown">
                    <mvvmlightextra:EventToCommand Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Path=DataContext.CmdDownKeyDown}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <wpftk:DataGrid.Columns>
                <wpftk:DataGridTemplateColumn Header="Name" IsReadOnly="False">
                    <wpftk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Ts1}" x:Name="txtName" />
                        </DataTemplate>
                    </wpftk:DataGridTemplateColumn.CellTemplate>

                </wpftk:DataGridTemplateColumn>
                <wpftk:DataGridTextColumn Binding="{Binding Path=Ts2}" Header="Ts2" IsReadOnly="False"/>
            </wpftk:DataGrid.Columns>
        </owpfc:FFDataGrid>
之后我尝试

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
    {



        if (e.Key == System.Windows.Input.Key.Down)
        {
            if (SelectedIndex < Items.Count)
            {
                SelectedIndex++;
                DataGridCell cell = GetCell(SelectedIndex, 0);
                var currentFirstControl = (FrameworkElement)cell.Content;
                currentFirstControl.Focus();
            }


        }
        else if (e.Key == System.Windows.Input.Key.Up)
        {
            if (SelectedIndex > 0)
            {
                SelectedIndex--;
                DataGridCell cell = GetCell(SelectedIndex, 1);
                var currentFirstControl = (FrameworkElement) cell.Content;
                currentFirstControl.Focus();
            }


        }

        base.OnPreviewKeyDown(e);
    }

真的需要你的帮助,thx !!

0 个答案:

没有答案