wpf 在 DataGrid 中设置焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21481861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set focus inside DataGrid
提问by Beetlejuice
I Have a DataGrid with some data loaded. I need to set focus in the first row of this grid.
我有一个加载了一些数据的 DataGrid。我需要在这个网格的第一行设置焦点。
I will call this method from a button onClick event.
我将从按钮的 onClick 事件中调用此方法。
The myGrid.Focus()does not focus the rows inside the grid.
该myGrid.Focus()不集中行的网格内。
采纳答案by Mojtaba
It is one of the hardest job in WPF, and reason is because of Virtualization and the fact that UI Render thread is different from the thread that runs our code (you can not find when exactly UI render finished). for a complete reference you can have look here http://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a-datagrid.aspx
这是 WPF 中最难的工作之一,原因是因为虚拟化以及 UI 渲染线程与运行我们代码的线程不同的事实(您无法找到 UI 渲染何时完成)。要获得完整的参考,您可以在这里查看 http://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a -datagrid.aspx
using Dispatcher.Invoke might works for some situations (believe me in WPF Dispatcher.Invoke is your best friend and biggest enemy)
使用 Dispatcher.Invoke 可能适用于某些情况(相信我在 WPF Dispatcher.Invoke 是你最好的朋友和最大的敌人)
dgGrid.ItemsSource = new List<object>() { new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 } };
Dispatcher.Invoke(new Action(delegate()
{
grd.SelectedIndex = 0;
grd.Focus();
}
), System.Windows.Threading.DispatcherPriority.Background);
or robustest one from linked article
或链接文章中最强大的
public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
if (rowContainer != null)
{
DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
/* if the row has been virtualized away, call its ApplyTemplate() method
* to build its visual tree in order for the DataGridCellsPresenter
* and the DataGridCells to be created */
rowContainer.ApplyTemplate();
presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
}
if (presenter != null)
{
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
if (cell == null)
{
/* bring the column into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
}
return cell;
}
}
return null;
}
public static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
{
if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");
if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
dataGrid.SelectedItems.Clear();
/* set the SelectedItem property */
object item = dataGrid.Items[rowIndex]; // = Product X
dataGrid.SelectedItem = item;
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
if (row == null)
{
/* bring the data item (Product object) into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(item);
row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
}
if (row != null)
{
DataGridCell cell = GetCell(dataGrid, row, 0);
if(cell != null)
cell.Focus();
}
}
you need to add these static methods call the second one it first tries tries find (or draw the row if it is draw yet then set focus on it).
您需要添加这些静态方法,调用它第一次尝试查找的第二个方法(如果尚未绘制行,则绘制该行,然后将焦点放在它上面)。
回答by Anudas Mohan
Try this:
尝试这个:
if (e.Key == Key.Up || e.Key == Key.Down)
{
var uiElement = e.OriginalSource as UIElement;
dgvRoute.Focus(); //Datagrid
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
回答by thoros1179
mygrid.SelectedItem = mygrid.Items.Count > 0 ? mygrid.Items[0] : null;
回答by Satish
If you want to focus first value in grid try following
如果您想关注网格中的第一个值,请尝试以下操作
myGrid.SelectedIndex = 0;

