【问题标题】:Set focus inside DataGrid在 DataGrid 中设置焦点
【发布时间】:2014-01-31 13:57:47
【问题描述】:

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

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

myGrid.Focus() 不关注网格内的行。

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    这是 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 是你最好的朋友和最大的敌人)

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

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

    【讨论】:

      【解决方案2】:

      试试这个:

      if (e.Key == Key.Up || e.Key == Key.Down) 
      { 
          var uiElement = e.OriginalSource as UIElement; 
          dgvRoute.Focus(); //Datagrid 
          uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
      }
      

      【讨论】:

        【解决方案3】:
        mygrid.SelectedItem = mygrid.Items.Count > 0 ? mygrid.Items[0] : null;
        

        【讨论】:

        【解决方案4】:

        如果您想关注网格中的第一个值,请尝试关注

        myGrid.SelectedIndex = 0;
        

        【讨论】:

        • 这不关注网格。实际上,我们需要关注单元格,就像上面的示例一样。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多