datagrid怎么将焦点跑到指定的元素上
如上图,我想做一个简单的查找,已经可以找到指定的行和列,怎么让焦点跑到指定的元素上啊,
用SelectedIndex已经可以跑到指定的行了。。。
[解决办法]
int i = 0;
private void button2_Click(object sender, RoutedEventArgs e)
{
//i取具体的值行
DataGridRow dr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);第
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dr);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0); //取第0列每行单元格
cell.Focus();
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}