private void ShearchEan(string s_EAN) { try { for (int i = 0; i < DataGridSQL.Items.Count; i++) { DataGridRow row = (DataGridRow)DataGridSQL.ItemContainerGenerator.ContainerFromIndex(i); TextBlock cellContent = DataGridSQL.Columns[8].GetCellContent(row) as TextBlock; if (cellContent != null && cellContent.Text.Equals(s_EAN)) { object item = DataGridSQL.Items[i]; //DataGridSQL.SelectedItem = item; DataGridSQL.ScrollIntoView(item); row.Background = System.Windows.Media.Brushes.Pink; row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); Ean.Focus(); break; } } } catch (Exception e) { } }
private void HighlightCurrentStateInDatagrid() { try { Dispatcher.Invoke(() => { var stateObj = _viewModel.States.Where(name => name.StateName.ToUpper() == _viewModel.Diagnostics._currentStateName.ToUpper()).FirstOrDefault(); if (stateObj == null) { return; } var index = _viewModel.States.IndexOf(stateObj); dgStates.SelectedIndex = index; DataGridRow row = (DataGridRow)dgStates.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { return; } row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }); } catch { // swallow UI exception, if any } }
/// <summary> /// Wywoływane, gdy wystąpi zdarzenie MouseLeftButtonDown. /// </summary> /// <param name="e">System.Windows.Input.MouseButtonEventArgs, który zawiera dane zdarzenia. Dane zdarzenia informują o naciśnięciu lewego przycisku myszy.</param> protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (SelectionUnit != DataGridSelectionUnit.Cell) { Border border = e.OriginalSource as Border; if (border != null) { //var row = ItemContainerGenerator.ContainerFromItem(CurrentItem) as DataGridRow; DataGridRow dataGridRow = border.TemplatedParent as DataGridRow; if (dataGridRow != null) { if (Keyboard.IsKeyDown(Key.LeftCtrl)) { base.SelectedItems.Add(dataGridRow.Item); } else { DataGridSelectionUnit saveSelectionUnit = SelectionUnit; SelectionUnit = DataGridSelectionUnit.Cell; UnselectAll(); SelectionUnit = saveSelectionUnit; dataGridRow.IsSelected = true; dataGridRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } } } }
private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == Key.Enter) { BringToFront(); } else if (e.Key == Key.Down || e.Key == Key.Up) { if (!WindowsGrid.IsKeyboardFocusWithin) { Keyboard.Focus(WindowsGrid); DataGridRow row = (DataGridRow)WindowsGrid.ItemContainerGenerator.ContainerFromIndex(0); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } else if (e.Key == Key.Escape) { clearTextbox(); } else if (e.Key == Key.LWin || e.Key == Key.RWin) { return; } else { SearchBox.Focus(); } }
private void Grid_KeyDown(object sender, KeyEventArgs e) { TreeGridElement SelectedData = Grid.SelectedItem as TreeGridElement; if (SelectedData == null) { return; } if (e.Key == Key.Right && !SelectedData.IsExpanded) { SelectedData.IsExpanded = true; e.Handled = true; } else if (e.Key == Key.Left) { while (SelectedData != null) { if (SelectedData.IsExpanded) { SelectedData.IsExpanded = false; Grid.SelectedItem = SelectedData; Grid.ScrollIntoView(SelectedData); DataGridRow Row = (DataGridRow)Grid.ItemContainerGenerator.ContainerFromItem(SelectedData); Row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); e.Handled = true; break; } SelectedData = SelectedData.Parent as TreeGridElement; } } }
public static void SetFocusToGrid(DataGrid targetGrid, int index = -1) { // no items if (targetGrid.Items.Count < 1) { return; } // keep current row selected if (index == -1 && targetGrid.SelectedIndex > -1) { index = targetGrid.SelectedIndex; } // if no item selected, pick first if (index == -1) { index = 0; } targetGrid.SelectedIndex = index; // set full focus DataGridRow row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { targetGrid.UpdateLayout(); // scroll to view if outside targetGrid.ScrollIntoView(targetGrid.Items[index]); row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index); } // NOTE does this causes move below? row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); row.Focus(); Keyboard.Focus(row); }
private void DataGrid_BillItems_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (this._unloadedRow && this.BillDataGrid.SelectedIndex >= 0) { this._unloadedRow = false; DataGridRow selectedRow = (DataGridRow)this.BillDataGrid.ItemContainerGenerator.ContainerFromIndex(this.BillDataGrid.SelectedIndex); selectedRow?.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } }
private void GetFocusOnCategoryGrid() { if ((grdCategoryContent.Items.Count > 0) && (grdCategoryContent.Columns.Count > 0)) { dg1.SelectedIndex = 0; DataGridRow row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(0); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } }
void GetFocusOnDataGrid() { if ((dgTaxCodesAndRates.Items.Count > 0) && (dgTaxCodesAndRates.Columns.Count > 0)) { dgTaxCodesAndRates.SelectedIndex = 0; DataGridRow row = (DataGridRow)dgTaxCodesAndRates.ItemContainerGenerator.ContainerFromIndex(0); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } }
private void btnAuto_Click(object sender, RoutedEventArgs e) { dispatcherTimer.Stop(); string specialization = cmbSpecializations.Text; if (specialization == "" || cmbZone.Text == "") { cmbSpecializations.BorderBrush = Brushes.Red; cmbZone.BorderBrush = Brushes.Red; MessageBox.Show("Please select specialization and zone!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { cmbSpecializations.ClearValue(Border.BorderBrushProperty); cmbZone.ClearValue(Border.BorderBrushProperty); int faultZone = Convert.ToInt32(cmbZone.Text); autoAssignTechnician auto = new autoAssignTechnician(); Array result = auto.autoAssign(specialization, faultZone); if (result.GetValue(0).ToString() == "Error") { MessageBox.Show("Auto-assign couldn't find any matching technicians! Please try again!", "Fault report", MessageBoxButton.OK, MessageBoxImage.Error); } else { txtTechID.Text = result.GetValue(0).ToString(); txtTechName.Text = result.GetValue(1).ToString(); txtZone.Text = result.GetValue(2).ToString(); txtJobs.Text = result.GetValue(3).ToString(); foreach (System.Data.DataRowView dr in dgTechnicians.ItemsSource) { var value = Convert.ToInt32(dr[0]); int index = dgTechnicians.Items.IndexOf(dr); if (value == Convert.ToInt32(result.GetValue(0))) { dgTechnicians.ScrollIntoView(dgTechnicians.Items[index]); DataGridRow row = (DataGridRow)dgTechnicians.ItemContainerGenerator.ContainerFromIndex(index); TextBlock cellContent = dgTechnicians.Columns[1].GetCellContent(row) as TextBlock; object item = dgTechnicians.Items[index]; dgTechnicians.SelectedItem = item; dgTechnicians.ScrollIntoView(item); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } MessageBox.Show("Technician found!" + "\n" + "\nTechincian ID: " + result.GetValue(0).ToString() + "\nName: " + result.GetValue(1).ToString(), "Fault report", MessageBoxButton.OK, MessageBoxImage.Information); dispatcherTimer.Start(); } } }
protected override void OnCurrentCellChanged(EventArgs e) { if (this.CurrentCell.Column != null) { if (this.CurrentCell.Column != null && this.CurrentCell.Column.DisplayIndex == this.Columns.Count() - 1) { //PressKey(Key.Return); DataGridCell cell = Helper.GetCell(CurrentCell); int index = Helper.GetRowIndex(cell); DataGridRow dgrow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(this.Items[index]); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); } } }
private void focusSelectedItem() { if (SelectedItem != null) { DataGridRow row = (DataGridRow)ItemContainerGenerator.ContainerFromIndex(SelectedIndex); if (row == null) { row = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as DataGridRow; } row?.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } }
private void FindService(string Type, string Sign) { foreach (DataRowView Nazv in dg1.ItemsSource) { if (Nazv[Type].ToString() == Sign) { dg1.SelectedIndex = dg1.Items.IndexOf(Nazv); dg1.ScrollIntoView(dg1.SelectedItem); DataGridRow row = (DataGridRow)dg1.ItemContainerGenerator.ContainerFromIndex(dg1.SelectedIndex); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); //Finder - textbox , collect combobox } } }
/// <summary> /// Programitically select DataGrid Row /// Reference - https://stackoverflow.com/questions/1976087/wpf-datagrid-set-selected-row /// </summary> /// <param name="gridItemIndex"></param> private void SelectDataGridItemByRowIndex(int gridItemIndex) { try { DataGridRow row = (DataGridRow)dgPDFs.ItemContainerGenerator.ContainerFromIndex(gridItemIndex); object item = dgPDFs.Items[gridItemIndex]; dgPDFs.SelectedItem = item; dgPDFs.ScrollIntoView(item); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } catch (Exception ex) { Utils.WriteErrorMessageToFile(ex.ToString(), EnableErrorLog); } }
private void gamesGrid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e) { DataGrid grid = (DataGrid)sender; if (e.Command == DataGrid.DeleteCommand) { int index = gamesGrid.SelectedIndex; foreach (Game g in gamesGrid.SelectedItems) { if (!g.Locked) { dataFile.Games.Remove(g); } } // refresh grid view gamesGrid.CommitEdit(); CollectionViewSource.GetDefaultView(gamesGrid.ItemsSource).Refresh(); // update header headerName.Text = dataFile.Header.Name; headerDesc.Text = dataFile.Header.Description; headerGameCount.Content = "(" + gamesGrid.Items.Count + " games)"; // auto-select next row if (index >= gamesGrid.Items.Count) { index = gamesGrid.Items.Count - 1; } if (index >= 0) { object item = gamesGrid.Items[index]; if (item != null) { gamesGrid.SelectedItem = item; gamesGrid.ScrollIntoView(item); DataGridRow row = (DataGridRow)gamesGrid.ItemContainerGenerator.ContainerFromIndex(index); if (row != null) { row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } } // ok e.Handled = true; } }
private void SelectAt(int index, bool isNew = false) { object item = dataGrid.Items[index]; if (isNew) { dataGrid.SelectedItem = item; dataGrid.ScrollIntoView(item); DataGridRow dgrow = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item); dgrow.MoveFocus(new System.Windows.Input.TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next)); } else { dataGrid.SelectedItem = item; dataGrid.ScrollIntoView(item); } }
private void SelectAccountInDataGrid(string id) { for (int i = 0; i < dgAccountList.Items.Count; i++) { dgAccountList.ScrollIntoView(dgAccountList.Items[i]); DataGridRow row = (DataGridRow)dgAccountList.ItemContainerGenerator.ContainerFromIndex(i); TextBlock cellContent = dgAccountList.Columns[0].GetCellContent(row) as TextBlock; if (cellContent != null && cellContent.Text.Equals(id)) { object item = dgAccountList.Items[i]; dgAccountList.SelectedItem = item; dgAccountList.ScrollIntoView(item); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); break; } } }
private void ProcessSetSelectedRow() { if (!IsProcessSelected) { return; } // Отримаємо index обраного item (Process): int index = -1; foreach (MyData myItem in dgProcesses.Items) { index++; if (myItem.Id == selectedProcessId) { break; } } // Після сортування індекси змінилися, теперь index нашого item вже інший. // Ми повинні почистити стару строку (та item) і встановити заново. dgProcesses.SelectedItems.Clear(); // Отримуємо наш item вже з новым index: object item = dgProcesses.Items[index]; dgProcesses.SelectedItem = item; // Знаходимо Grid Row, в якому знаходиться наш item: DataGridRow row = (DataGridRow)dgProcesses.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dgProcesses.ScrollIntoView(item); row = dgProcesses.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow; dgProcesses.UpdateLayout(); } row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
private void btnAdd_Click(object sender, RoutedEventArgs e) { //add blank row var itemsSource = dataGrid.ItemsSource as ObservableCollection <ItemModel>; if (itemsSource != null) { itemsSource.Add(new ItemModel()); } //SetFocusToFirstCell(); int index = 0; dataGrid.SelectedItem = dataGrid.Items[index]; dataGrid.ScrollIntoView(dataGrid.Items[index]); DataGridRow dgrow = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
private void ModuleSetSelectedRow() { if (!IsModuleSelected) { return; } int index = -1; foreach (MyData myItem in dgModules.Items) { index++; if (myItem.Id == selectedModuleId) { break; } } dgModules.SelectedItems.Clear(); object item = dgModules.Items[index]; dgModules.SelectedItem = item; DataGridRow row = (DataGridRow)dgModules.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dgModules.ScrollIntoView(item); row = dgModules.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow; dgModules.UpdateLayout(); } row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
public static void WinDataGridSelectRow(Int32 id, DataGrid winDg, WindowMetaList windowMetaList) //Selects the row in the data grid for the current id { int x = windowMetaList.GridSelectedIndex; try { object item = winDg.Items[x]; winDg.SelectedItem = item; winDg.ScrollIntoView(item); winDg.UpdateLayout(); DataGridRow row = (DataGridRow)winDg.ItemContainerGenerator.ContainerFromIndex(x); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } catch (Exception ex) { WindowTasks.DisplayError(ex, "Problem Selecting the DataGrid Row:", null); } }
private void CommitChangeClick(object sender, RoutedEventArgs e) { if (StudentsDataGrid.SelectedItem == null) { var G = new DataBase.Students(); G.F = SurnameTextBox.Text; G.I = NameTextBox.Text; G.O = PatronymicTextBox.Text; G.Groups = (DataBase.Groups)GroupsComboBox.SelectedItem; Students.Add(G); SourceCore.DB.Students.Add(G); StudentsDataGrid.ScrollIntoView(G); StudentsDataGrid.SelectedIndex = Students.Count - 1; DataGridRow row = (DataGridRow)StudentsDataGrid.ItemContainerGenerator.ContainerFromIndex(Students.Count - 1); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } SourceCore.DB.SaveChanges(); CloseChangeFieldClick(sender, e); }
private void ISINsearch_TextChanged(object sender, TextChangedEventArgs e) { try { for (int i = 0; i < DGIsinQuote.Items.Count; i++) { DataGridRow row = (DataGridRow)DGIsinQuote.ItemContainerGenerator.ContainerFromIndex(i); TextBlock cellContent = DGIsinQuote.Columns[0].GetCellContent(row) as TextBlock; if (cellContent != null && cellContent.Text.Equals(ISINsearch.Text)) { object item = DGIsinQuote.Items[i]; DGIsinQuote.SelectedItem = item; DGIsinQuote.ScrollIntoView(item); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); break; } } } catch (Exception) { } }
private void triggerClick() { for (int i = 0; i < clickEventList.Count; i++) { ClickEvent currentEvnet = clickEventList[i]; Dispatcher.BeginInvoke(new Action(() => { clickEventDataGrid.Items.MoveCurrentTo(currentEvnet); logOutClickEvent(currentEvnet); DataGridRow row = (DataGridRow)clickEventDataGrid.ItemContainerGenerator.ContainerFromIndex(i); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); })); invokeClickEvent(currentEvnet); } //sim.Mouse.MoveMouseTo(XScaleRate * x, YScaleRate * y); //sim.Mouse.LeftButtonClick(); setButtonContent("Activate"); }
private void Ctrl_DataGrid_Words_KeyUp(object sender, KeyEventArgs e) { //Todo get indexex of keyboard focused cell DataGrid ctrl = sender as DataGrid; if (ctrl == null) { return; } if (e.Key != Key.Enter) { return; } ctrl.SelectedItem = ctrl.Items[ctrl.SelectedIndex]; ctrl.ScrollIntoView(ctrl.SelectedItem); DataGridRow dgRow = (DataGridRow)ctrl.ItemContainerGenerator.ContainerFromItem(ctrl.SelectedItem); dgRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); ctrl.CurrentCell = new DataGridCellInfo(ctrl.Items[ctrl.SelectedIndex], ctrl.Columns[2]); ctrl.BeginEdit(); }
private void SelectedUnion_OnPreviewKeyDown(object sender, KeyEventArgs e) { var args = e; var key = args.Key; if (SelectedUnion.ItemsSource != null && SelectedUnion.SelectedItem != null) { var selectedindex = ConnectionList.SelectedIndex; var selectedchanelineIndex = SelectedUnion.SelectedIndex; var selectedchanel = (Chanel)SelectedUnion.SelectedItem; var maxindex = Connections.ElementAt(ConnectionList.SelectedIndex).Chanels.Count - 1; if (key == Key.Up && selectedchanelineIndex != 0) { Connections.ElementAt(selectedindex).Chanels.Remove(selectedchanel); Connections.ElementAt(selectedindex).Chanels.Insert(selectedchanelineIndex - 1, selectedchanel); SelectedUnion.ItemsSource = null; SelectedUnion.ItemsSource = Connections.ElementAt(ConnectionList.SelectedIndex).Chanels; SelectedUnion.SelectedItem = SelectedUnion.Items[selectedchanelineIndex]; SelectedUnion.ScrollIntoView(SelectedUnion.Items[selectedchanelineIndex]); DataGridRow dgrow = (DataGridRow)SelectedUnion.ItemContainerGenerator.ContainerFromItem(SelectedUnion.Items[selectedchanelineIndex]); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); } if (key == Key.Down && selectedchanelineIndex != maxindex) { Connections.ElementAt(selectedindex).Chanels.Remove(selectedchanel); Connections.ElementAt(selectedindex).Chanels.Insert(selectedchanelineIndex + 1, selectedchanel); SelectedUnion.ItemsSource = null; SelectedUnion.ItemsSource = Connections.ElementAt(ConnectionList.SelectedIndex).Chanels; SelectedUnion.SelectedItem = SelectedUnion.Items[selectedchanelineIndex]; SelectedUnion.ScrollIntoView(SelectedUnion.Items[selectedchanelineIndex]); DataGridRow dgrow = (DataGridRow)SelectedUnion.ItemContainerGenerator.ContainerFromItem(SelectedUnion.Items[selectedchanelineIndex]); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down)); } } }
private void DeleteClick(object sender, RoutedEventArgs e) { if (MessageBox.Show("Удалить запись?", "Внимание!", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { var obj = (DataBase.Students)StudentsDataGrid.SelectedItem; int i = StudentsDataGrid.SelectedIndex; if (--i < 0) { i = i + 2; } if (i < StudentsDataGrid.Items.Count) { StudentsDataGrid.ScrollIntoView(obj); StudentsDataGrid.SelectedIndex = i; DataGridRow row = (DataGridRow)StudentsDataGrid.ItemContainerGenerator.ContainerFromIndex(i); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } SourceCore.DB.Students.Remove(obj); Students.Remove(obj); SourceCore.DB.SaveChanges(); } }
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 = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
private void MoveUpButton_OnClick(object sender, RoutedEventArgs e) { if (listObject.Count != 0) { if (datagrid.SelectedIndex == 0) { DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(datagrid.SelectedIndex); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } else if (datagrid.SelectedIndex == -1) // trường hợp chưa chọn dòng nào { datagrid.SelectedIndex = listObject.Count - 1; datagrid.Focus(); } else { datagrid.SelectedIndex--; DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(datagrid.SelectedIndex); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } }
private void Presentacion_datagrid_DropDownClosed(object sender, EventArgs e) { ComboBox comboBox = sender as ComboBox; if (comboBox.SelectedIndex > -1) { if (verificarTabla() == false) { datos.Add(new fila() { numero = "", presentacion = presentaciones }); try { DataGridRow dataGridRow = (DataGridRow)tabla_Principal.ItemContainerGenerator.ContainerFromIndex(tabla_Principal.SelectedIndex); dataGridRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down)); tabla_Principal.SelectedIndex++; } catch { } } } }