简体   繁体   English

将焦点移动到WPF中的下一个控件

[英]Move Focus to next Control in WPF

Suppose I have a DataGrid and a Button . 假设我有一个DataGrid和一个Button CanUserAddRows is set to True . CanUserAddRows设置为True Let this dataGrid have two DataGridTextColumns namely "Name" and "Age". 让这个dataGrid有两个DataGridTextColumns即“Name”和“Age”。

Now if user takes the following actions : 现在,如果用户执行以下操作:

  1. He adds name as XYZ and Age as 20. Then he press enter. 他将名字添加为XYZ,将年龄添加为20.然后按Enter键。 So a new row will be added to the datagrid. 因此,将在datagrid中添加一个新行。
  2. He adds name as ABC and Age as 12. Then he press enter. 他将姓名改为ABC,年龄为12岁。然后他按回车键。 So a new row will be added to the datagrid. 因此,将在datagrid中添加一个新行。
  3. He keeps name empty and press Enter or TAB then I want to move focus to Button instead of next cell of datagrid. 他保持名称为空并按Enter或TAB然后我想将焦点移动到Button而不是datagrid的下一个单元格。

I have watched many questions but I don't get the logic of if user left the name empty and how do I move focus to Button instead of next cell. 我已经看了很多问题,但是如果用户将名称留空,我不会得到逻辑,如何将焦点移到Button而不是下一个单元格。

Use DataGridView.SelectedCells[0] so you can retrieve the value of the selected cell (assuming you can only select one). 使用DataGridView.SelectedCells[0]以便您可以检索所选单元格的值(假设您只能选择一个)。

To get the actual string inside, you will have to cast the content to a proper WPF object, like TextBlock. 要获取实际的字符串,您必须将内容强制转换为正确的WPF对象,如TextBlock。 myCell.Column.GetCellContent(cell.Item) as TextBlock

Then in a PreviewKeyDown event handler (KeyDown having known issues in DataGridView), you can use button.Focus() . 然后在PreviewKeyDown事件处理程序(在DataGridView中具有已知问题的 KeyDown)中,您可以使用button.Focus() ( more about those issues ) 更多关于这些问题

//...
myDataGrid1.PreviewKeyDown += myDataGrid1_KeyDown;
//...
void myDataGrid1_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        var cell = myDataGrid1.SelectedCells[0];
        TextBlock cellContent = cell.Column.GetCellContent(cell.Item) as TextBlock;
        if (cellContent != null)
        {
            if (String.IsNullOrWhitespace(cellContent.Text))
                button.Focus();
        }
    }
}

About getting the column's name, it's another question, for which you can find answer here for example. 关于获取列的名称,这是另一个问题,例如,您可以在找到答案。

As a side note, you're not really supposed to interact directly with a DataGridView cells' values, since it's meant to be bound with a data source from which you should retrieve the data you want to test. 作为旁注,您并不应该直接与DataGridView单元格的值进行交互,因为它意味着与数据源绑定,您应该从中检索要测试的数据。 However, you can search a bit for helper methods that can help you get what you want . 但是,您可以搜索一下帮助您获得所需内容的辅助方法。

You can define a handler for the DataGrid.KeyDown event, as: 您可以为DataGrid.KeyDown事件定义处理程序,如下所示:

void myDataGrid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        button.Focus();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM