Esempio n. 1
0
 /// <summary>
 /// 改变焦点
 /// </summary>
 /// <param name="changeType">0:获取默认,1:获取下一行,2:获取上一行</param>
 public void GetGridFocus(int changeType = 0)
 {
     if (changeType == 1)
     {
         if (dataGrid.SelectedIndex > 0)
         {
             dataGrid.SelectedIndex -= 1;
         }
     }
     else if (changeType == 2)
     {
         dataGrid.SelectedIndex += 1;
     }
     else
     {
         dataGrid.SelectedIndex = 0;
     }
     dataGrid.Focus();
     if (dataGrid.SelectedItem == null)
     {
         return;
     }
     System.Windows.Controls.DataGridCell cell = (dataGrid.Columns[0].GetCellContent(dataGrid.SelectedItem))?.Parent as System.Windows.Controls.DataGridCell; if (cell == null)
     {
         return;
     }
     cell.IsTabStop = false;
     cell.Focus();
 }
Esempio n. 2
0
        /// <summary>
        /// Selects the row by indexes.
        /// </summary>
        /// <param name="dataGrid">The data grid.</param>
        /// <param name="rowIndexes">The row indexes.</param>
        /// <exception cref="ArgumentException">
        /// The SelectionUnit of the DataGrid must be set to FullRow.
        /// or
        /// The SelectionMode of the DataGrid must be set to Extended.
        /// or
        /// Invalid number of indexes.
        /// or
        /// </exception>
        public static void SelectRowByIndexes(this DataGrid dataGrid, params int[] rowIndexes)
        {
            if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
            {
                throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");
            }

            if (!dataGrid.SelectionMode.Equals(DataGridSelectionMode.Extended))
            {
                throw new ArgumentException("The SelectionMode of the DataGrid must be set to Extended.");
            }

            if (rowIndexes.Length.Equals(0) || rowIndexes.Length > dataGrid.Items.Count)
            {
                throw new ArgumentException("Invalid number of indexes.");
            }

            dataGrid.SelectedItems.Clear();
            foreach (int rowIndex in rowIndexes)
            {
                if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
                {
                    throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
                }

                object item = dataGrid.Items[rowIndex]; //=Product X
                dataGrid.SelectedItems.Add(item);

                DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
                if (row == null)
                {
                    dataGrid.ScrollIntoView(item);
                    row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
                }

                if (row != null)
                {
                    System.Windows.Controls.DataGridCell cell = GetCell(dataGrid, row, 0);
                    if (cell != null)
                    {
                        cell.Focus();
                    }
                }
            }
        }