6
A
回答
5
我認爲最好的辦法是使用Items屬性和直接訪問您的數據項:
var dataItem = dataGrid.Items[0] as ...;
,但你可以使用這個類來獲取細胞並用的GetValue訪問value()方法(會更像你的例子)。從這裏取
static class DataGridHelper {
static public DataGridCell GetCell(DataGrid dg, int row, int column) {
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null) {
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
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;
}
static public 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;
}
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;
}
}
3
一般情況下,你不應該需要做的。在WPF中,datagrid意味着與數據綁定一起使用,這意味着存在與單元格具有相同值的底層集合或對象,因此您需要直接訪問該集合/對象。如果您需要訪問單元格值,則可能需要重新考慮設計。
相關問題
- 1. WPF的DataGrid,在細胞
- 2. WPF的DataGrid:讓細胞只讀
- 3. C#WPF專注於細胞的DataGrid
- 4. 在WPF中動態添加組合框在Datagrid細胞中
- 5. 更改單個細胞的基於細胞的DataGrid中值的顏色
- 6. 更改DataGrid細胞
- 7. 在海洋中獲取細胞一側?
- 8. 獲取datagrid列值 - WPF C#
- 9. 獲取值從gridview的細胞datatbase
- 10. 在WPF中獲取datagrid的datagridrow
- 11. 獲取細胞的indexPath.row
- 12. 的ListView /在細胞多行的DataGrid
- 13. 獲取在WPF的DataGrid
- 14. 將在編輯模式下一個DataGrid中選定行的所有細胞WPF
- 15. 如何在DataGrid中創建同一列不同類型的細胞 - WPF C#
- 16. WPF的DataGrid變髒細胞的數據沒有提交
- 17. 從範圍獲取細胞
- 18. 獲取單元格的值,其中分離細胞匹配
- 19. WPF的DataGrid - retemplating細胞導致綁定不更新
- 20. 獲得細胞ODF JTable中
- 21. 如何在C#WPF DataGrid中獲取選定的行值?
- 22. 提取在細胞
- 23. Excel中,提取細胞
- 24. 從DataGrid獲得單個細胞的含量在Flex 3的
- 25. GTK中的自定義樹細胞渲染器 - 如何獲取細胞數據?
- 26. 獲取DataGrid中選定的行項WPF
- 27. 的DataGrid CellEditending事件不會在細胞返回更新值
- 28. 刷新WPF DataGrid而不會丟失細胞聚焦
- 29. 從WPF Datagrid獲取單元格值(在vb中)
- 30. 從wpf datagrid獲取隱藏值
正如我告訴每個winforms開發人員,我發現他的頭對WPF ...忘了你從winforms中學到的一切,這是一個不同的(更好的IMO)框架,需要一個完全不同的思維模式。看看MVVM並熟悉WPF綁定功能 –