public static void SingleClickEditing(object gridCell) { DataGridCell cell = gridCell as DataGridCell; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = FindVisualParent <DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = FindVisualParent <DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } }
/// <summary> /// 修改为单击进入编辑状态 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = (DataGridCell)sender; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid grdData = FindVisualParent <DataGrid>(cell); if (grdData.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = FindVisualParent <DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } }
private void rightClick(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while (dep != null && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } DataGridCell cell = dep as DataGridCell; cell.Focus(); while (dep != null && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; GridSongs.SelectedItem = row.DataContext; var song = (Song)GridSongs.SelectedItems[0]; var index = song.Count - 1; string path = songDictionary[index]; songListContext.setPath(path); }
protected override void OnPreviewKeyDown(KeyEventArgs e) { base.OnPreviewKeyDown(e); Int32 row = base.Items.IndexOf(base.CurrentItem); Int32 Col = base.Columns.IndexOf(base.CurrentColumn); // 向下方向键 if (e.Key == Key.Down) { if (base.SelectedIndex < base.Items.Count - 1) { DataGridCell dgc = this.GetCell(row, Col); dgc.IsSelected = true; dgc.Focus(); } } // 向上方向键 else if (e.Key == Key.Up) { if (base.SelectedIndex > 0) { DataGridCell dgc = this.GetCell(row, Col); dgc.IsSelected = true; dgc.Focus(); } } // Tab键 else if (e.Key == Key.Tab) { if (i == 0 && Col == 0) { var dataGridCellInfo = new DataGridCellInfo(this.Items[row], this.Columns[Col]); this.CurrentCell = dataGridCellInfo; i++; } else if (i == 1 && Col == 1) { DataGridCell dgc = this.GetCell(row, Col); dgc.IsSelected = true; dgc.Focus(); i++; } else { if (row > -1 && row < this.Items.Count && Col >= 0 && Col < this.Columns.Count - 1) { var dataGridCellInfo = new DataGridCellInfo(this.Items[row], this.Columns[Col + 1]); this.CurrentCell = dataGridCellInfo; i = 0; } // 聚焦到每行最后一列,聚焦到下一行第一列,最后一行除外 else if (row > -1 && row < this.Items.Count - 1 && Col == this.Columns.Count - 1) { var dataGridCellInfo = new DataGridCellInfo(this.Items[row + 1], this.Columns[0]); this.CurrentCell = dataGridCellInfo; i = 0; } } } }
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; // Set focus for single click editing if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = FindParent <DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = FindParent <DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } }
protected override void OnPreviewKeyDown(KeyEventArgs e) { base.OnPreviewKeyDown(e); DataGridCell senderCell = e.OriginalSource as DataGridCell; bool ctrlDown = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl); if (e.Key == Key.Return) { if (senderCell != null && !senderCell.IsEditing) { // Enter edit mode if current cell is not in edit mode senderCell.Focus(); this.BeginEdit(); e.Handled = true; } } else if (e.Key == Key.Space) { if (senderCell != null && !senderCell.IsEditing) { object item = senderCell.DataContext; // In some cases senderCell is not selected. This can happen after multi selection over all items. if (!senderCell.IsSelected) { item = SelectedItem; // simply use first selected item } ToggleEnabledForItem(item); e.Handled = true; } } else if (ctrlDown && e.Key == Key.Up) { if (MoveUpCommand != null && MoveUpCommand.CanExecute(null)) { var focusedCellItem = (Keyboard.FocusedElement as DataGridCell)?.DataContext; MoveUpCommand.Execute(null); // DataGrid loses keyboard focus after moving items FocusCellAfterDelay(focusedCellItem); } e.Handled = true; } else if (ctrlDown && e.Key == Key.Down) { if (MoveDownCommand != null && MoveDownCommand.CanExecute(null)) { var focusedCellItem = (Keyboard.FocusedElement as DataGridCell)?.DataContext; MoveDownCommand.Execute(null); // DataGrid loses keyboard focus after moving items FocusCellAfterDelay(focusedCellItem); } e.Handled = true; } }
public void SetSelectedCell(DataGridCell Cell) { if (Cell == null || Cell.IsEditing || Cell.IsReadOnly) { return; } if (!Cell.IsFocused) { Cell.Focus(); } if (this.EditingDataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!Cell.IsSelected) { Cell.IsSelected = true; } } else { var row = Cell.GetNearestVisualDominantOfType <DataGridRow>(); if (row != null && !row.IsSelected) { row.IsSelected = true; } } }
public static void SelectCell(DataGrid dataGrid, DataGridCell cell) { bool extendSelection = dataGrid.SelectionMode == DataGridSelectionMode.Extended; bool minimalModify = ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control); if (!extendSelection) { return; } if (minimalModify) { //DataGridCellInfo info = new DataGridCellInfo(cell); //dataGrid1.SelectedCells.Add(info); cell.IsSelected = true; } else { dataGrid.UnselectAllCells(); //DataGridCellInfo info = new DataGridCellInfo(cell); //dataGrid1.SelectedCells.Add(info); cell.IsSelected = true; } cell.Focus(); return; }
//this method programmatically selects a row on the datagrid used for book sorting 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(); object item = dataGrid.Items[rowIndex]; dataGrid.SelectedItem = 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) { DataGridCell cell = GetCell(dataGrid, row, 0); if (cell != null) { cell.Focus(); } } }
private DataGridRow SelectionOnRightClick(MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return(null); } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow dataRow = dep as DataGridRow; return(dataRow); } return(null); }
/// <summary>Sends a request to activate a control and initiate its single, unambiguous action.</summary> // Token: 0x060025EA RID: 9706 RVA: 0x000B5990 File Offset: 0x000B3B90 void IInvokeProvider.Invoke() { this.EnsureEnabled(); object item = base.Item; if (this.GetWrapperPeer() == null) { this.OwningDataGrid.ScrollIntoView(item); } bool flag = false; UIElement wrapper = base.GetWrapper(); if (wrapper != null) { IEditableCollectionView items = this.OwningDataGrid.Items; if (items.CurrentEditItem == item) { flag = this.OwningDataGrid.CommitEdit(); } else if (this.OwningDataGrid.Columns.Count > 0) { DataGridCell dataGridCell = this.OwningDataGrid.TryFindCell(item, this.OwningDataGrid.Columns[0]); if (dataGridCell != null) { this.OwningDataGrid.UnselectAll(); dataGridCell.Focus(); flag = this.OwningDataGrid.BeginEdit(); } } } if (!flag && !this.IsNewItemPlaceholder) { throw new InvalidOperationException(SR.Get("DataGrid_AutomationInvokeFailed")); } }
/// <summary> /// 选中DataGrid中的行 /// 如果在表格中有其他响应鼠标事件的组件, 会导致grid的row无法响应 /// 此处使用响应的组件找到其所属的行并选中 /// </summary> /// <param name="grid">表格</param> /// <param name="e">事件</param> public static void SelectRow(DataGrid grid, RoutedEventArgs e) { grid.SelectedItem = null; DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; grid.SelectedItem = row.DataContext; } }
public void MydataGride_MouseRightClick(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; ((DataGrid)sender).SelectedItem = row.DataContext; var columnIndex = ((DataGrid)sender).Columns.IndexOf(((DataGrid)sender).CurrentColumn); object item = ((DataGrid)sender).SelectedItem; string ID = (((DataGrid)sender).SelectedCells[columnIndex].Column.GetCellContent(item) as TextBlock).Text; MessageBox.Show(ID); } }
private void SetCurrentRow(int rowIndex) { dataGrid.SelectionUnit = DataGridSelectionUnit.FullRow; 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; } else { DataGridCell cell = GetCell(dataGrid, row, 0); if (cell != null) { cell.Focus(); } } }
public bool SetFocusOnSelectedRow(object selected, string columnHeader) { if ((this.ParentMenu != null && this.ParentMenu.IsOpen) || (this.ContextMenu != null && this.ContextMenu.IsOpen)) { // fix bug where context menu disappears when you right click on a new item. return(false); } // the focus this row so that user can continue using keyboard navigation DataGridRow row = this.ItemContainerGenerator.ContainerFromItem(selected) as DataGridRow; if (row != null) { int columnToFocus = GetColumnIndexByTemplateHeader(columnHeader); if (columnToFocus < 0) { return(false); } DataGridCellInfo dgci = this.SelectedCells[columnToFocus]; if (dgci != null) { int rowIndex = row.GetIndex(); int colIndex = GetColIndex(dgci); row.ApplyTemplate(); DataGridCell dgc = GetCell(rowIndex, colIndex); if (dgc != null) { dgc.Focus(); return(true); } } } return(false); }
public static void SetFocusOnNewCreatedColumn(DataGrid DataGrid, int rowIndex) { DataGrid.Focus(); DataGridRow rowContainer = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; if (rowContainer == null) { DataGrid.SelectedIndex = rowIndex; DataGrid.ScrollIntoView(DataGrid.SelectedItem); rowContainer = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; } if (rowContainer != null) { rowContainer.ApplyTemplate(); DataGridCellsPresenter presenter = WpfTools.FindVisualChild <DataGridCellsPresenter>(rowContainer); DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell; if (cell == null) { /* bring the column into view in case it has been virtualized away */ DataGrid.ScrollIntoView(rowContainer, DataGrid.Columns[0]); cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell; } if (cell != null) { cell.Focus(); } } }
public bool SetFocusOnSelectedRow(object selected, string columnHeader) { // the focus this row so that user can continue using keyboard navigation DataGridRow row = this.ItemContainerGenerator.ContainerFromItem(selected) as DataGridRow; if (row != null) { int columnToFocus = GetColumnIndexByTemplateHeader(columnHeader); if (columnToFocus < 0) { return(false); } DataGridCellInfo dgci = this.SelectedCells[columnToFocus]; if (dgci != null) { int rowIndex = row.GetIndex(); int colIndex = GetColIndex(dgci); row.ApplyTemplate(); DataGridCell dgc = GetCell(rowIndex, colIndex); if (dgc != null) { dgc.Focus(); return(true); } } } return(false); }
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Down) { e.Handled = true; TextBox tb = sender as TextBox; DataGridRow dgr = (DataGridRow)grdUnSelJudge.ItemContainerGenerator.ContainerFromIndex(0); if (dgr == null) { return; } grdUnSelJudge.SelectedIndex = 0; grdUnSelJudge.Focus(); DataGridCellsPresenter presenter = AthleticsCommon.GetVisualChild <DataGridCellsPresenter>(dgr); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(1); if (cell != null) { cell.Focus(); } //dgr.Focus(); } }
void Window1_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; dataGrid1.SelectedItem = row.DataContext; } }
/// <summary> /// Отображение изменяемых ячеек в форму для изменения. /// </summary> /// <param name="dg"></param> /// <param name="row"></param> private void _showCellsEditingTemplate(DataGrid dg, DataGridRow row) { try { foreach (DataGridColumn col in dg.Columns) { if (col.Visibility != Visibility.Hidden) { DependencyObject parent = VisualTreeHelper.GetParent(col.GetCellContent(row)); while (parent.GetType().Name != "DataGridCell") { parent = VisualTreeHelper.GetParent(parent); } DataGridCell cell = ((DataGridCell)parent); DataGridTemplateColumn c = (DataGridTemplateColumn)col; if (c.CellEditingTemplate != null) { cell.Content = ((DataGridTemplateColumn)col).CellEditingTemplate.LoadContent(); } cell.Focus(); } } } catch (Exception e) { MessageBox.Show(e.Message); } }
void Window1_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; dataService.SelectedItem = row.DataContext; Model.view.ServiceView RowData = (Model.view.ServiceView)dataService.SelectedItem; Disable_MenuItem.Header = RowData.STATUS == "Disabled" ? "Active" : "Disable Room"; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/63fa1e10-1050-4448-a2bc-62dfe0836f25/selecting-datagrid-row-when-right-mouse-button-is-pressed?forum=wpf private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; //depがRunだと、VisualTreeHelper.GetParent()で下記の例外が投げられてしまう。 //'System.Windows.Documents.Run' is not a Visual or Visual3D' InvalidOperationException if (e.OriginalSource is Run run) { dep = run.Parent; } while ((dep != null) && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; cell.Focus(); while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; //dataGrid.SelectedItem = row.DataContext; } }
/// <summary> /// Allows one click editing. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param> private void DataGridCell_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = FindVisualParent <DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = FindVisualParent <DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } }
//public static void SelectRowByIndex (DataGrid dataGrid, int rowIndex, int GetCellindex) //{ // if (!dataGrid.SelectionUnit.Equals (DataGridSelectionUnit.FullRow)) // //Add dbselector call in here somewhere // { // Console.WriteLine ("The SelectionUnit of the DataGrid must be set to FullRow."); // return; // } // if (dataGrid.Items.Count == 0) // return; // if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1)) // { // Console.WriteLine (string.Format ("Positioning error - {0} is an invalid row index.", rowIndex)); // return; // } // //Crashes if the grid is set to single selecton only // /* 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); // //if dataGrid = "DataGrid1" we are handling EditDb DataGrid // // else it is "BankGrid" or CustomerGrid or DetailsGrid in SQLDbViewer // row = dataGrid.ItemContainerGenerator.ContainerFromIndex (rowIndex) as DataGridRow; // } // if (GetCellindex != -1) // { // DataGridCell cell = GetCell (dataGrid, row, GetCellindex); // if (cell != null) // cell.Focus (); // // TODO: Retrieve and focus a DataGridCell object // } //} //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, int GetCellindex) { DataGrid caller = null; DataGrid slaveGrid = null; if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow)) { Console.WriteLine("The SelectionUnit of the DataGrid must be set to FullRow."); return; } // if(dataGrid == MainWindow.DgControl.CurrentSqlGrid) // dataGrid = MainWindow.DgControl.SelChangeCallerGrid; Console.WriteLine($"SelectRowByIndex: Caller = {dataGrid.Name}, RowToFind = {rowIndex}"); if (dataGrid == null) { return; } caller = dataGrid; if (dataGrid.Items.Count == 0) { return; } if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1)) { Console.WriteLine(string.Format("Positioning error - {0} is an invalid row index.", rowIndex)); return; } //Crashes if the grid is set to single selecton only /* set the SelectedItem property */ object item = dataGrid.Items[rowIndex]; 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); //if dataGrid = "DataGrid1" we are handling EditDb DataGrid // else it is "BankGrid" or CustomerGrid or DetailsGrid in SQLDbViewer //row = dataGrid.ItemContainerGenerator.ContainerFromIndex (rowIndex) as DataGridRow; } if (GetCellindex != -1) { DataGridCell cell = GetCell(dataGrid, row, GetCellindex); if (cell != null) { cell.Focus(); } // TODO: Retrieve and focus a DataGridCell object } dataGrid.SelectedItem = dataGrid.SelectedIndex; dataGrid.ScrollIntoView(dataGrid.SelectedItem); //clear flag again // MainWindow.DgControl.SelChangeCallerGrid = null; }
// https://softwaremechanik.wordpress.com/2013/10/02/how-to-make-all-wpf-datagrid-cells-have-a-single-click-to-edit/ public static void DataGridSingleClickHack(DependencyObject originalSource) { // If the user clicked the TextBlock // use single click edit only for the last, empty row if (originalSource is TextBlock) { if (!(originalSource is FrameworkElement)) { return; } object item = (originalSource as FrameworkElement).DataContext; if (!(item is VariableItem)) { return; } VariableItem variable = item as VariableItem; if (variable.Name != null && variable.Name != "") { return; } } // Find corresponding cell and row DataGridCell cell = null; DataGridRow row = null; { DependencyObject parent = originalSource; while (parent != null) { if (parent is DataGridCell) { cell = parent as DataGridCell; } else if (parent is DataGridRow) { row = parent as DataGridRow; break; } parent = VisualTreeHelper.GetParent(parent); } } // Do magic stuff if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } // NOTE: assuming SelectionUnit == FullRow if (row != null && !row.IsSelected) { row.IsSelected = true; } } }
private void FocusRow(DataGridRow row) { DataGridCell cell = row.FindDescendant <DataGridCell>(); if (cell != null) { cell.Focus(); } }
public static void FocusCell(DataGrid dataGrid, int row, int column) { DataGridCell cell = GetCell(dataGrid, row, column); if (cell != null) { cell.Focus(); } }
public override void GotoLineExecuted(object sender) { FilterTabViewModel filterTab = (FilterTabViewModel)CurrentTab(); LogTabViewModel logTab = (LogTabViewModel)_LogViewModel.CurrentTab(); if (filterTab != null && logTab != null) { int filterIndex = -1; int logIndex = ((Selector)logTab.Viewer).SelectedIndex; if (logIndex <= logTab.ContentList.Count) { filterIndex = logTab.ContentList[logIndex].FilterIndex; } else { SetStatus("filter:gotoLine:error in index:" + filterIndex.ToString()); return; } if (filterIndex >= 0) { SetStatus("filter:gotoLine:" + filterIndex.ToString()); ((Selector)filterTab.Viewer).SelectedIndex = filterIndex; DataGrid dataGrid = (DataGrid)CurrentTab().Viewer; FilterFileItem filterFileItem = CurrentFile().ContentItems.FirstOrDefault(x => x.Index == filterIndex); dataGrid.ScrollIntoView(filterFileItem); dataGrid.SelectedItem = filterFileItem; dataGrid.SelectedIndex = dataGrid.Items.IndexOf(filterFileItem); dataGrid.UpdateLayout(); DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex); DataGridCellsPresenter presenter = FindVisualChild <DataGridCellsPresenter>(row); if (presenter != null) { DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell; if (cell != null) { cell.Focus(); } } } else if (filterIndex == -1) { // quick filter if (QuickFindCombo != null) { Keyboard.Focus(QuickFindCombo); } } } }
/// <summary> /// forcus vào bản ghi (tên datagrid, dòng , cột) /// </summary> /// <param name="grid">tên grid</param> /// <param name="row">row muốn forcus</param> /// <param name="column">cột</param> public static void NVSFocus(this DataGrid grid, int row, int column) { try { DataGridCell _dgc = DataGridHelper.GetCell(grid, row, column); //grid.Items.Refresh(); if (_dgc != null) { _dgc.Focus(); grid.SelectedIndex = row; } else { _dgc.Focus(); grid.SelectedIndex = row; } } catch { } }
// Focuses and selects cells ahead of time to allow single-click editing of data grid cells. private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsFocused) { cell.Focus(); cell.IsSelected = true; } }