简体   繁体   English

关闭弹出消息后如何将焦点重新设置到错误元素?

[英]How to set focus back to error element after closing a popup message?

I have a DataGrid that has a DataGridTemplateColumn contains an editable TextBox control. 我有一个DataGrid,其中的DataGridTemplateColumn包含可编辑的TextBox控件。 I also register LostFocus event in xaml using EventTrigger and InvokeCommandAction. 我还使用EventTrigger和InvokeCommandAction在xaml中注册LostFocus事件。

For example: My grid has two rows, when I click the cell of the first row to edit something, if I enter some invalid values the press Tab or Enter key to focus out of editing cell, I have to do the following tasks: 例如:我的网格有两行,当我单击第一行的单元格来编辑内容时,如果输入一些无效值,请按Tab或Enter键将焦点移出编辑单元格,我必须执行以下任务:

  1. Make the border of that cell is Red 使该单元格的边框为红色
  2. Show an error message popup (just contains a message label and a close button) 显示错误消息弹出窗口(仅包含消息标签和关闭按钮)
  3. After the above popup closed, set focus to that error cell and put a pointer to textbox inside it for user to input again until they enter a valid value 在上述弹出窗口关闭后,将焦点设置到该错误单元格,并在其中放置指向文本框的指针,以供用户再次输入,直到输入有效值为止

About No.1 & 2 I have done but I don't know how to do with No.3. 关于1号和2号我已经做过,但我不知道该如何处理3号。 Current behavior is when the popup closed, it automatically focus to cell of the next row instead of previous error cell. 当前行为是关闭弹出窗口时,它将自动聚焦到下一行的单元格而不是上一个错误单元格。

Can anyone help me? 谁能帮我? Thank you. 谢谢。

Finally, I found a way can resolve my issue, that is using Dispatcher object to invoke a delegate like this: 最后,我找到了一种解决我的问题的方法,即使用Dispatcher对象调用这样的委托:

var focusingElement = (System.Threading.ThreadStart)delegate { element.Focus(); };
Application.Current.Dispatcher.BeginInvoke(focusingElement);

Where is element is a type of FrameworkElement or UIElement object. 其中elementFrameworkElementUIElement对象的类型。 In my case that is TextBox control. 就我而言,这是TextBox控件。

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

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