데이터그리드 정보 가져오는 헬퍼
public static class DataGridViewHelper
{
public static DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); // <<<------ ERROR
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public static DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}
사용법
DataGridCell cell = DataGridViewHelper.GetCell(grid, rowIndex, cellIndex);
DataGridRow row = DataGridViewHelper.GetRow(grid, rowIndex);
'WPF' 카테고리의 다른 글
07. WPF DataGrid Click 위치로 정보 가져오기 (0) | 2020.08.21 |
---|---|
04. WPF MVVM(3) RelayCommand (0) | 2020.08.21 |
03. WPF MVVM(2) INotifyPropertyChanged (0) | 2020.08.21 |
02. WPF MVVM(1) View-ViewModel 연결 (0) | 2020.08.21 |
01. WPF DataGrid Text Center (0) | 2020.05.30 |