WPF datagrid寻找下一列里面的textbox
时间: 2024-01-20 17:47:15 浏览: 88
可以使用VisualTreeHelper类的FindVisualChild方法找到DataGridRow中的Textbox,再获取下一列的Textbox。以下是示例代码:
```
public static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindVisualChild<T>(child);
if (result != null)
return result;
}
}
return null;
}
private void GetNextTextBox()
{
var row = dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex) as DataGridRow;
if (row != null)
{
// 获取当前选中单元格的列索引
var columnIndex = dataGrid.CurrentColumn.DisplayIndex;
// 获取下一列的Textbox
var nextTextBox = FindVisualChild<TextBox>(row)
.Where(tb => Grid.GetColumn(tb) == columnIndex + 1)
.FirstOrDefault();
if (nextTextBox != null)
{
nextTextBox.Focus();
}
}
}
```
当然以上代码只是示例,需要根据具体情况进行修改。
阅读全文