가능한 중복 :
Select DataGridCell from DataGridWPF에서 DataGrid의 셀 값을 가져 오는 방법은 무엇입니까?
나는 약간의 열과 행 WPF의 데이터 그리드가있다. 행을 클릭하면 선택한 행의 첫 번째 열을 가져 오려고합니다. 내가 어떻게 해? LINQ를 사용할 수 있습니까? 고맙습니다
가능한 중복 :
Select DataGridCell from DataGridWPF에서 DataGrid의 셀 값을 가져 오는 방법은 무엇입니까?
나는 약간의 열과 행 WPF의 데이터 그리드가있다. 행을 클릭하면 선택한 행의 첫 번째 열을 가져 오려고합니다. 내가 어떻게 해? LINQ를 사용할 수 있습니까? 고맙습니다
당신은 단순히이 확장
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
을 방법 - 사용할 수 있으며 (귀하의 경우 0) 기존 행과 열 ID로 데이터 그리드의 셀을 얻을 수 있습니다 :
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;
}
확인 자세한 내용은이 링크 - Get WPF DataGrid row and cell
var firstSelectedCellContent = this.dataGrid.Columns[0].GetCellContent(this.dataGrid.SelectedItem);
var firstSelectedCell = firstSelectedCellContent != null ? firstSelectedCellContent.Parent as DataGridCell : null;
당신이 할 수있는이 방법 DataGridCell 및 DataGridCell 자체의 내용 인 FrameworkElement를 가져옵니다.
DataGrid에 EnableColumnVirtualization = True
이 있으면 위의 코드에서 null 값이 나타날 수 있습니다.
특정 DataGridCell에 대해 데이터 소스의 실제 값을 가져 오려면 좀 더 복잡합니다. DataGridCell은 백업 데이터 소스의 여러 값 (속성)으로 구성 될 수 있으므로 일반적으로이 작업을 수행 할 수있는 방법이 없으므로 특정 DataGridColumn에 대해이 작업을 처리해야합니다.
[this] (http://stackoverflow.com/q/9978119/995246) 질문을 참조하십시오. – gliderkite