Exemple #1
0
        /// <summary>
        /// 将SelectedItem滚动为第一行
        /// </summary>
        /// <param name="dataGrid">目标DagaGrid</param>
        /// <param name="selectedItem">选中项</param>
        public static void SetSelectedItemFirstRow(object dataGrid, object selectedItem)
        {
            //若目标datagrid为空,抛出异常
            if (dataGrid == null)
            {
                throw new ArgumentNullException("目标无" + dataGrid + "无法转换为DataGrid");
            }
            //获取目标DataGrid,为空则抛出异常
            System.Windows.Controls.DataGrid dg = dataGrid as System.Windows.Controls.DataGrid;
            if (dg == null)
            {
                throw new ArgumentNullException("目标无" + dataGrid + "无法转换为DataGrid");
            }
            //数据源为空则返回
            if (dg.Items == null || dg.Items.Count < 1)
            {
                return;
            }

            //首先滚动为末行
            dg.SelectedItem  = dg.Items[dg.Items.Count - 1];
            dg.CurrentColumn = dg.Columns[0];
            dg.ScrollIntoView(dg.SelectedItem, dg.CurrentColumn);

            //获取焦点,滚动为目标行
            dg.Focus();
            dg.SelectedItem  = selectedItem;
            dg.CurrentColumn = dg.Columns[0];
            dg.ScrollIntoView(dg.SelectedItem, dg.CurrentColumn);
        }
 public static System.Windows.Controls.DataGridCell GetCell(System.Windows.Controls.DataGrid dataGrid, System.Windows.Controls.DataGridRow rowContainer, int column)
 {
     if (rowContainer != null)
     {
         System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = FindVisualChild <System.Windows.Controls.Primitives.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 <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
         }
         if (presenter != null)
         {
             System.Windows.Controls.DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as System.Windows.Controls.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 System.Windows.Controls.DataGridCell;
             }
             return(cell);
         }
     }
     return(null);
 }
        public static void SelectRowByIndex(System.Windows.Controls.DataGrid dataGrid, int rowIndex)
        {
            if (!dataGrid.SelectionUnit.Equals(System.Windows.Controls.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;

            System.Windows.Controls.DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as System.Windows.Controls.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 System.Windows.Controls.DataGridRow;
            }
            //TODO: Retrieve and focus a DataGridCell object
        }
 public static void SelectDataGridItem(System.Windows.Controls.DataGrid dataGrid,
                                       int index)
 {
     if (0 <= index && index < dataGrid.Items.Count)
     {
         object item = dataGrid.Items[index];
         dataGrid.CurrentItem  = item;
         dataGrid.SelectedItem = item;
         dataGrid.ScrollIntoView(item);
     }
     else
     {
         dataGrid.CurrentItem  = null;
         dataGrid.SelectedItem = null;
     }
 }
        public static void SelectCellsByIndexes(System.Windows.Controls.DataGrid dataGrid, IList <KeyValuePair <int, int> > cellIndexes)
        {
            if (!dataGrid.SelectionUnit.Equals(System.Windows.Controls.DataGridSelectionUnit.Cell))
            {
                throw new ArgumentException("The SelectionUnit of the DataGrid must be set to Cell.");
            }

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

            dataGrid.SelectedCells.Clear();
            foreach (KeyValuePair <int, int> cellIndex in cellIndexes)
            {
                int rowIndex    = cellIndex.Key;
                int columnIndex = cellIndex.Value;

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

                if (columnIndex < 0 || columnIndex > (dataGrid.Columns.Count - 1))
                {
                    throw new ArgumentException(string.Format("{0} is an invalid column index.", columnIndex));
                }

                object item = dataGrid.Items[rowIndex]; //= Product X
                System.Windows.Controls.DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as System.Windows.Controls.DataGridRow;
                if (row == null)
                {
                    dataGrid.ScrollIntoView(item);
                    row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as System.Windows.Controls.DataGridRow;
                }
                if (row != null)
                {
                    System.Windows.Controls.DataGridCell cell = GetCell(dataGrid, row, columnIndex);
                    if (cell != null)
                    {
                        System.Windows.Controls.DataGridCellInfo dataGridCellInfo = new System.Windows.Controls.DataGridCellInfo(cell);
                        dataGrid.SelectedCells.Add(dataGridCellInfo);
                        cell.Focus();
                    }
                }
            }
        }
        public void AddTask(System.Windows.Controls.DataGrid tasksGrid)
        {
            //GetTimeRemaining()
            SelectedTask = InternalAddTask(String.Format("Task {0}", tasks.Count + 1), ColorInfo.GetRandomColor(), new TimeInfoViewModel());
            if (tasksGrid != null)
            {
                tasksGrid.ScrollIntoView(selectedTask);
                var cellInfo = new System.Windows.Controls.DataGridCellInfo(selectedTask, tasksGrid.Columns[1]);
                int row      = tasksGrid.Items.IndexOf(selectedTask);
                tasksGrid.CurrentCell = cellInfo;
                var cell = tasksGrid.GetCell(row, 1);
                if (cell != null)
                {
                    cell.Focus();
                }
                tasksGrid.BeginEdit();
            }

            NotifyOfPropertyChange(() => CanAddTask);
            NotifyOfPropertyChange(() => CanStartTasks);
            NotifyOfPropertyChange(() => CanAddTasks);
            CalculateValidationErrors();
        }