using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Media; using System.Windows.Controls; using System.Windows.Controls.Primitives;namespace WpfApplication1 { public static class DataGridHelper { /// <summary> /// Gets the visual child of an element /// </summary> /// <typeparam name="T">Expected type</typeparam> /// <param name="parent">The parent of the expected element</param> /// <returns>A visual child</returns> 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; } /// <summary> /// Gets the specified cell of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <param name="row">The row of the cell</param> /// <param name="column">The column index of the cell</param> /// <returns>A cell of the DataGrid</returns> public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column) { if (row != null) { DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); if (presenter == null) { grid.ScrollIntoView(row, grid.Columns[column]); presenter = GetVisualChild<DataGridCellsPresenter>(row); } DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); return cell; } return null; } /// <summary> /// Gets the specified cell of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <param name="row">The row index of the cell</param> /// <param name="column">The column index of the cell</param> /// <returns>A cell of the DataGrid</returns> public static DataGridCell GetCell(this DataGrid grid, int row, int column) { DataGridRow rowContainer = grid.GetRow(row); return grid.GetCell(rowContainer, column); } /// <summary> /// Gets the specified row of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <param name="index">The index of the row</param> /// <returns>A row of the DataGrid</returns> public static DataGridRow GetRow(this DataGrid grid, int index) { DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { // May be virtualized, bring into view and try again. grid.UpdateLayout(); grid.ScrollIntoView(grid.Items[index]); row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); } return row; } /// <summary> /// Gets the selected row of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <returns></returns> public static DataGridRow GetSelectedRow(this DataGrid grid) { return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem); } } }//主程序 private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Enter) { e.Handled = true; Int32 row = dataGrid1.Items.IndexOf(dataGrid1.CurrentItem); Int32 Col = dataGrid1.Columns.IndexOf(dataGrid1.CurrentColumn); var cell = DataGridHelper.GetCell(dataGrid1, row, Col+1); if (cell != null) { cell.IsSelected = true; cell.Focus(); //dataGrid1.BeginEdit(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
if (e.Key == Key.Enter)
{
Int32 row= dataGrid1.Items.IndexOf(dataGrid1.CurrentItem);
Int32 Col = dataGrid1.Columns.IndexOf(dataGrid1.CurrentColumn);
}
谢谢,此代码不错。。有个问题是:wpf datagrid 不能捕获entry键。。请问有方法解决吗?
private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Int32 row = dataGrid1.Items.IndexOf(dataGrid1.CurrentItem);
Int32 Col = dataGrid1.Columns.IndexOf(dataGrid1.CurrentColumn);
//dataGrid1.SelectedItem = dataGrid1.Columns[Col+1]
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;namespace WpfApplication1
{
public static class DataGridHelper
{
/// <summary>
/// Gets the visual child of an element
/// </summary>
/// <typeparam name="T">Expected type</typeparam>
/// <param name="parent">The parent of the expected element</param>
/// <returns>A visual child</returns>
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;
} /// <summary>
/// Gets the specified cell of the DataGrid
/// </summary>
/// <param name="grid">The DataGrid instance</param>
/// <param name="row">The row of the cell</param>
/// <param name="column">The column index of the cell</param>
/// <returns>A cell of the DataGrid</returns>
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
/// <summary>
/// Gets the specified cell of the DataGrid
/// </summary>
/// <param name="grid">The DataGrid instance</param>
/// <param name="row">The row index of the cell</param>
/// <param name="column">The column index of the cell</param>
/// <returns>A cell of the DataGrid</returns>
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
DataGridRow rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
/// <summary>
/// Gets the specified row of the DataGrid
/// </summary>
/// <param name="grid">The DataGrid instance</param>
/// <param name="index">The index of the row</param>
/// <returns>A row of the DataGrid</returns>
public static DataGridRow GetRow(this DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// May be virtualized, bring into view and try again.
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
/// <summary>
/// Gets the selected row of the DataGrid
/// </summary>
/// <param name="grid">The DataGrid instance</param>
/// <returns></returns>
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
}
}//主程序
private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{ try
{
if (e.Key == Key.Enter)
{
e.Handled = true;
Int32 row = dataGrid1.Items.IndexOf(dataGrid1.CurrentItem);
Int32 Col = dataGrid1.Columns.IndexOf(dataGrid1.CurrentColumn);
var cell = DataGridHelper.GetCell(dataGrid1, row, Col+1);
if (cell != null)
{
cell.IsSelected = true;
cell.Focus();
//dataGrid1.BeginEdit();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}