/ / Wybierz DataGridCell z DataGrid - c #, .net, wpf, wpfdatagrid, datagridcell

Wybierz DataGridCell z DataGrid - c #, .net, wpf, wpfdatagrid, datagridcell

mam DataGrid Kontrola WPF i chcę uzyskać konkretne DataGridCell. Znam indeksy wierszy i kolumn. Jak mogę to zrobić?

potrzebuję DataGridCell ponieważ muszę mieć dostęp do jego treści. Więc jeśli mam (na przykład) kolumnę DataGridTextColummoja treść będzie TextBlock obiekt.

Odpowiedzi:

4 dla odpowiedzi № 1

Możesz użyć kodu podobnego do tego, aby wybrać komórkę:

var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[rowNo], dataGrid.Columns[colNo]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;

Nie widzę sposobu na zaktualizowanie zawartości konkretnej komórki bezpośrednio, więc aby zaktualizować zawartość konkretnej komórki, wykonuję następujące czynności

// gets the data item bound to the row that contains the current cell
// and casts to your data type.
var item = dataGrid.CurrentItem as MyDataItem;

if(item != null){
// update the property on your item associated with column "n"
item.MyProperty = "new value";
}
// assuming your data item implements INotifyPropertyChanged the cell will be updated.

2 dla odpowiedzi nr 2

Możesz po prostu użyć tej metody rozszerzenia-

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

i można uzyskać komórkę DataGrid przez istniejący identyfikator wiersza i kolumny:

public static DataGridCell GetCell(this DataGrid grid, 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);
}

DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}

grid.ScrollIntoView jest kluczem do tego, aby działało to w przypadku wirtualizacji DataGrid, a wymagana komórka nie jest obecnie dostępna.

Sprawdź ten link, by zobaczyć szczegóły - Uzyskaj wiersz i komórkę DataGrid WPF


1 dla odpowiedzi nr 3

Oto kod, którego użyłem:

    /// <summary>
/// Get the cell of the datagrid.
/// </summary>
/// <param name="dataGrid">The data grid in question</param>
/// <param name="cellInfo">The cell information for a row of that datagrid</param>
/// <param name="cellIndex">The row index of the cell to find. </param>
/// <returns>The cell or null</returns>
private DataGridCell TryToFindGridCell(DataGrid dataGrid, DataGridCellInfo cellInfo, int cellIndex = -1)
{
DataGridRow row;
DataGridCell result = null;

if (dataGrid != null && cellInfo != null)
{
if (cellIndex < 0)
{
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
}
else
{
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(cellIndex);
}

if (row != null)
{
int columnIndex = dataGrid.Columns.IndexOf(cellInfo.Column);

if (columnIndex > -1)
{
DataGridCellsPresenter presenter = this.FindVisualChild<DataGridCellsPresenter>(row);

if (presenter != null)
{
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
else
{
result = null;
}
}
}
}

return result;
}`

Zakłada to, że DataGrid została już załadowana (wykonywana jest własna Załadowana procedura obsługi).