1个回答
展开全部
我在前台放了一个datagrid和一个button。button用来循环使datagrid的第0列的单元格获取焦点。
后台没乎事件腊梁如下:
int i = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (i < myDataGrid.Items.Count)
{
DataGridRow dr = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(i);
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dr);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0); //取枯局悉第0列每行单元格
cell.Focus();
i++;
}
else
{
i = 0;
}
}
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;
}
后台没乎事件腊梁如下:
int i = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (i < myDataGrid.Items.Count)
{
DataGridRow dr = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(i);
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dr);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0); //取枯局悉第0列每行单元格
cell.Focus();
i++;
}
else
{
i = 0;
}
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询