public static int GetRowIndex(DataGridCell dataGridCell) { // Use reflection to get DataGridCell.RowDataItem property value. System.Reflection.PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); DataGrid dataGrid = GetDataGridFromChild(dataGridCell); return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null)); }
/// <summary> /// Creates a structure that identifies the specific cell container. /// </summary> /// <param name="cell"> /// A reference to a cell. /// This structure does not maintain a strong reference to the cell. /// Changes to the cell will not affect this structure. /// </param> /// <remarks> /// This constructor will tie the DataGridCellInfo to the specific /// DataGrid that owns the cell container. /// </remarks> public DataGridCellInfo(DataGridCell cell) { if (cell == null) { throw new ArgumentNullException("cell"); } _item = cell.RowDataItem; _column = cell.Column; _owner = new WeakReference(cell.DataGridOwner); }
public static Microsoft.Windows.Controls.DataGridCell GetCell(this Microsoft.Windows.Controls.DataGrid grid, Microsoft.Windows.Controls.DataGridRow row, int column) { if (row != null) { DataGridCellsPresenter presenter = GetVisualChild <DataGridCellsPresenter>(row); if (presenter == null) { grid.ScrollIntoView(row, grid.Columns[column]); presenter = GetVisualChild <DataGridCellsPresenter>(row); } Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); return(cell); } return(null); }
/// <summary> /// Creates the visual tree that will become the content of a cell. /// </summary> protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { return(LoadTemplateContent(/* isEditing = */ true, dataItem, cell)); }
/// <summary> /// Creates the visual tree that will become the content of a cell. /// </summary> /// <param name="isEditing">Whether the editing version is being requested.</param> /// <param name="dataItem">The data item for the cell.</param> /// <param name="cell">The cell container that will receive the tree.</param> private FrameworkElement LoadTemplateContent(bool isEditing, object dataItem, DataGridCell cell) { DataTemplate template = ChooseCellTemplate(isEditing); DataTemplateSelector templateSelector = ChooseCellTemplateSelector(isEditing); if (template != null || templateSelector != null) { ContentPresenter contentPresenter = new ContentPresenter(); BindingOperations.SetBinding(contentPresenter, ContentPresenter.ContentProperty, new Binding()); contentPresenter.ContentTemplate = template; contentPresenter.ContentTemplateSelector = templateSelector; return(contentPresenter); } return(null); }