/// <summary> /// 获取单元格所在行 /// </summary> /// <param name="dataGridCell"></param> /// <param name="dataGrid"></param> /// <returns></returns> public static int GetRowIndex(DataGridCell dataGridCell, DataGrid dataGrid = null) { if (dataGridCell == null) { return(-1); } var rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic); if (rowDataItemProperty == null) { return(-1); } try { if (dataGrid == null) { dataGrid = GetDataGridFromChild(dataGridCell); } } catch { return(-1); } return(dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null))); }
/// <summary> /// Получение индекса строки для ячейки /// </summary> /// <param name="dataGridCell">Ячейка</param> /// <returns></returns> public static int GetRowIndex(DataGridCell dataGridCell) { PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic); DataGrid dataGrid = GetDataGridFromChild(dataGridCell); return(dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null))); }
//private DataTemplate CreateCellTemplate() //{ // DataTemplate template = new DataTemplate(); // FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid)); // // rows // var row1 = new FrameworkElementFactory(typeof(RowDefinition)); // var row2 = new FrameworkElementFactory(typeof(RowDefinition)); // gridFactory.AppendChild(row1); // gridFactory.AppendChild(row2); // // columns // var col1 = new FrameworkElementFactory(typeof(ColumnDefinition)); // col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(100, GridUnitType.Pixel)); // var col2 = new FrameworkElementFactory(typeof(ColumnDefinition)); // col2.SetValue(ColumnDefinition.WidthProperty, new GridLength(100, GridUnitType.Pixel)); // gridFactory.AppendChild(col1); // gridFactory.AppendChild(col2); // template.VisualTree = gridFactory; // return template; //} /// <summary> /// /// </summary> /// <param name="cell"></param> /// <returns></returns> public int GetRowIndex(DataGridCell cell) { // Use reflection to get DataGridCell.RowDataItem property value. PropertyInfo rowDataItemProperty = cell.GetType(). GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic); return(Items.IndexOf(rowDataItemProperty.GetValue(cell, null))); }
public static int GetRowIndex(DataGridCell dataGridCell) { // Use reflection to get DataGridCell.RowDataItem property value. PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic); DataGrid dataGrid = GetDataGridFromChild(dataGridCell); return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null)); }