The background implementation of WPF scrolls the currently selected items of DataGrid by pressing the number key

Posted by arag0rn on Sat, 16 May 2020 16:32:11 +0200

Recently, I encountered an item without mouse on the device. The interface is a DataGrid with full screen. It needs to scroll up and down the currently selected row of DataGrid by pressing 0 and 1 of the keypad

For important reference, https://blog.csdn.net/sinat_/article/details/105428496 implements background scrolling to the currently selected item.
Now take a note of the main implementation:

First of all, the front desk must be set with virtualizing StackPanel. Isvirtualizing = "false"

Next, there are two common methods:

  

/// <summary>
        /// take SelectedItem Scroll to first line
        /// </summary>
        /// <param name="dataGrid">target DagaGrid</param>
        /// <param name="selectedItem">Checked items</param>
        public static void SetSelectedItemFirstRow(object dataGrid, object selectedItem)
        {
            //If target datagrid Empty, throw exception
            if (dataGrid == null)
            {
                throw new ArgumentNullException("Target none" + dataGrid + "Cannot convert to DataGrid");
            }
            //Get target DataGrid,If it is empty, an exception will be thrown
            System.Windows.Controls.DataGrid dg = dataGrid as System.Windows.Controls.DataGrid;
            if (dg == null)
            {
                throw new ArgumentNullException("Target none" + dataGrid + "Cannot convert to DataGrid");
            }
            //If the data source is empty, return
            if (dg.Items == null || dg.Items.Count < 1)
            {
                return;
            }

            //Get focus, scroll to target line
            dg.Focus();
            dg.SelectedItem = selectedItem;
            dg.CurrentColumn = dg.Columns[0];
            dg.ScrollIntoView(dg.SelectedItem, dg.CurrentColumn);
        }

        /// <summary>
        /// Get selected row
        /// </summary>
        /// <param name="datagrid"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public static DataGridRow GetDataGridRow(DataGrid datagrid, int rowIndex)
        {
            DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
            if (row == null)
            {
                datagrid.UpdateLayout();

                row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
                row.IsSelected = true;
            }
            return row;
        } 

Then, add a KeyDown event for the form:

  

private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            dgvDeal.Focus();
            if (e.Key==Key.NumPad0)
            {
                if (dgvDeal.SelectedIndex == dgvDeal.Items.Count-1)
                {
                    dgvDeal.SelectedIndex = 0; 
                }
                else
                {
                    dgvDeal.SelectedIndex = dgvDeal.SelectedIndex + 1; 
                }
            }
            else if (e.Key == Key.NumPad1)
            {
                if (dgvDeal.SelectedIndex == 0)
                {
                    dgvDeal.SelectedIndex = dgvDeal.Items.Count-1; 
                }
                else
                {
                    dgvDeal.SelectedIndex = dgvDeal.SelectedIndex - 1; 
                }
            }
              
            DataGridRow resRow = GetDataGridRow(dgvDeal, dgvDeal.SelectedIndex);
            resRow.IsSelected = true;
            SetSelectedItemFirstRow(dgvDeal, dgvDeal.SelectedItem);
             
        }

 

          OK!

Topics: Windows