public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { object prop; try { System.Windows.Controls.DataGridCell cell = value as System.Windows.Controls.DataGridCell; System.Windows.Controls.DataGridBoundColumn column = cell.Column as System.Windows.Controls.DataGridBoundColumn; string PropertyPath = (column.Binding as System.Windows.Data.Binding).Path.Path; if (PropertyPath.Contains('.')) { prop = cell.DataContext.GetType().GetProperty(PropertyPath.Split('.')[0]).PropertyType; PropertyPath = PropertyPath.Split('.')[1]; } else { prop = cell.DataContext.GetType(); } Type type = (prop as Type).GetProperty(PropertyPath).PropertyType; if (type == typeof(decimal) || type == typeof(double) || type == typeof(int)) { return(TextAlignment.Right); } return(TextAlignment.Left); } catch (Exception ex) { return(TextAlignment.Left); } }
protected override System.Windows.FrameworkElement GenerateEditingElement(System.Windows.Controls.DataGridCell cell, object dataItem) { var comboBox = (System.Windows.Controls.ComboBox)base.GenerateEditingElement(cell, dataItem); comboBox.SelectionChanged += Combobox_SelectionChanged; return(comboBox); }
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 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(); } } } }
/// <summary>フォーカス取得時処理</summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGrid_GotFocus(object sender, RoutedEventArgs e) { if (dgJobConVariable.SelectedItems.Count < 1) { dgJobConVariable.SelectedItem = dgJobConVariable.Items[0]; } else { if (_isTabKey) { System.Windows.Controls.DataGridRow dgrow = (System.Windows.Controls.DataGridRow)dgJobConVariable.ItemContainerGenerator.ContainerFromItem(dgJobConVariable.Items[dgJobConVariable.SelectedIndex]); System.Windows.Controls.DataGridCell dgc = dgJobConVariable.Columns[0].GetCellContent(dgrow).Parent as System.Windows.Controls.DataGridCell; FocusManager.SetFocusedElement(dgJobConVariable, dgc as IInputElement); } } _isTabKey = false; }
public DataGridCellAutomationPeer(System.Windows.Controls.DataGridCell owner) : base(default(System.Windows.FrameworkElement)) { }