c# - 在 DataGrid 中设置焦点

标签 c# wpf datagrid

我有一个加载了一些数据的 DataGrid。我需要将焦点设置在该网格的第一行。

我将从按钮的 onClick 事件中调用此方法。

myGrid.Focus() 不会聚焦网格内的行。

最佳答案

这是 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

使用 Dispatcher.Invoke 可能适用于某些情况(相信我,WPF Dispatcher.Invoke 是你最好的 friend 也是最大的敌人)

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);

或链接文章中最强大的

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();
    }
 }

你需要添加这些静态方法调用第二个 它首先尝试查找(或绘制该行,如果它已绘制,然后将焦点放在它上面)。

关于c# - 在 DataGrid 中设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21481861/

相关文章:

c# - 从类、代码生成 XSD

c# - WPF 绑定(bind) TabControl TabItem

c# - 双倍值变化引起的火灾事件

Silverlight UI 文化

c# - 将 DataGrid.ItemSsource 转换为 DataTable

c# - 使用 dotnet 发布时设置 exe 文件版本

c# - C# 中的 SCOPE_IDENTITY - 范围

c# 将文件指针传递给非托管 c++ dll 以用于标准输出

c# - 聚焦时如何选择TextBox WPF中的所有文本?

javascript - jQuery GridView 类似于 Ext JS