Wpf DataGrid 的焦点问题
我正在使用 WPFToolKit DataGrid
来显示数据;在我的用例中,
- 用户单击一个单元格并输入一个值(我捕获该事件并将焦点设置到
TextBox
) - 按 Enter
- 键值已提交
- 焦点转移到下一个单元格
现在,用户无法输入TextBox
中的任何值(在 DataGridCell
中),除非他单击该单元格。 TextBox
可以是各种控件的一部分(例如 NumericUpDown
、Calendar
等)。
此行为类似于 Excel,但我无法将焦点转移到底层 TextBox
,因为 DataGridCell
中存在各种其他包装用户控件(类似于 DataGridCell
包含 MatrixCellContainer
,其中包含 MatrixCell
,其中包含 UpDown
控件)
任何指针都会非常有帮助。
更新:
我可以通过像这样处理 DataGridCell_Selected
事件来实现我正在寻找的内容:
private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
Microsoft.Windows.Controls.DataGridCell dataGridCell =
sender as Microsoft.Windows.Controls.DataGridCell;
// ToDo: This is a very bad hack;
// should be replaced by some proper technique
if (dataGridCell != null)
{
NumericUpDownBase<int>[] IntUpDownControls =
dataGridCell.GetChildrenOfType<NumericUpDownBase<int>>();
if (IntUpDownControls.Count() != 0)
{
IntUpDownControls[0].Focus();
//Keyboard.Focus(IntUpDownControls[0]);
}
else
{
NumericUpDownBase<double>[] DblUpDownControls =
dataGridCell.GetChildrenOfType<NumericUpDownBase<Double>>();
if (DblUpDownControls.Count() != 0)
{
DblUpDownControls[0].Focus();
//Keyboard.Focus(DblUpDownControls[0]);
}
}
}
}
但我知道会有更好的方法来实现这一点!
I am using WPFToolKit DataGrid
to display data; In my use case
- user clicks on a cell and enters a value (I trap the event and set the focus to the
TextBox
) - Presses Enter
- Value is committed
- Focus shifts to Next Cell
Now, user can't enter any value in the TextBox
(in the DataGridCell
) unless he clicks on the cell. The TextBox
can be a part of various controls(like NumericUpDown
, Calendar
etc.).
This behavior is similar to Excel but I am not able to shift the focus to the Underlying TextBox
as various other wrapper user controls are there in the DataGridCell
(something like DataGridCell
contains MatrixCellContainer
, which contains MatrixCell
, which contains UpDown
Control)
Any pointer will be really helpful.
Update:
I can achieve what I am looking for by handling DataGridCell_Selected
event like this :
private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
Microsoft.Windows.Controls.DataGridCell dataGridCell =
sender as Microsoft.Windows.Controls.DataGridCell;
// ToDo: This is a very bad hack;
// should be replaced by some proper technique
if (dataGridCell != null)
{
NumericUpDownBase<int>[] IntUpDownControls =
dataGridCell.GetChildrenOfType<NumericUpDownBase<int>>();
if (IntUpDownControls.Count() != 0)
{
IntUpDownControls[0].Focus();
//Keyboard.Focus(IntUpDownControls[0]);
}
else
{
NumericUpDownBase<double>[] DblUpDownControls =
dataGridCell.GetChildrenOfType<NumericUpDownBase<Double>>();
if (DblUpDownControls.Count() != 0)
{
DblUpDownControls[0].Focus();
//Keyboard.Focus(DblUpDownControls[0]);
}
}
}
}
But I know there will be a better way to achieve this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何将焦点设置到下一个单元格?
WPF 有两个版本的焦点:逻辑焦点和键盘焦点。我怀疑您正在使用
myDataGridCell.Focus()
,它仅设置逻辑焦点。How are you setting Focus to the next cell?
WPF has two versions of focus, Logical Focus and Keyboard Focus. I suspect you are using
myDataGridCell.Focus()
, which only sets Logical Focus.最后我解决了这个问题 -
但仍在寻找更好的方法......
Finally I settled with this -
Still looking for a better approach though...