有没有办法直接访问和操作指定 DataGrid 中的 DataGridCell?C# Visual Studio 2015 社区
我想访问特定的 DataGridCells,最好使用行和列,验证内容并更改一些属性。一旦我可以到达牢房,我就可以完成所有的工作。我一直在使用 VisualTreeHelper,几乎可以到达那里。我写“几乎”是因为由于某种原因,如果我在两个步骤之间插入 MessageBox.Show() 一切正常,除了我有一个我不想要的消息框。具体来说,我正在用一组数据填充数据网格,并希望更改每个单元格的背景和前景,这些单元格的数据与另一组匹配。
我在使用消息框、控制台写入以及本地和监视变量之间进行调试。我发现没有消息框,我的 DataGridRowPresenter 显示 0 个孩子并且我的条件失败。如果我删除条件,它会抛出一个索引越界异常。如果我有消息框,它会在消息框之前显示 0 个子节点,在消息框之后显示 2 个子节点(这对我的测试数据来说是正确的),并且该方法的其余部分继续执行并完全按照我的意愿执行。
我可以做些什么(除了消息框)来允许正确遍历树吗?或者,最好有一种更简单的方法可以直接进入我想要操作的单元格?如果我可以选择 DataGridViewer 而不是 DataGrid,这似乎很简单。
谢谢你,埃里克
PS 在 StackOverflow 上回答了几个非常相似的问题,但每个问题似乎都在解决不同 的问题如何在代码中访问 DataGridCell 的数据对象?- 正在寻找删除后的信息,我正在寻找基于行和列的单元格,而不是任何类型的事件 访问 WPF 中 DataGrid 的单元格值?- 是单元格值的一个很好的循环,但我似乎没有看到如何使用该过程来更改单元格属性(例如,如果单元格内容是“史密斯”,则将字体更改为粗体)
这就是我所做的...
// finds and returns a List<DependencyObject> containing datagrid row and cell based on sent datagrid, column, and row parameters
private List<DependencyObject> Get_DataGridCell_By_Index(DataGrid dataGrid, int row, int column)
{
bool isFound = false;
DataGridCell returnRow = new DataGridCell();
DependencyObject returnCell = new DependencyObject();
DependencyObject dependencyObject = dataGrid as DependencyObject;
List<DependencyObject> returnList = new List<DependencyObject>();
// to delete
int dependencyObjectChildrenCount;
// to delete
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is Border)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is ScrollViewer)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is Grid)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is ScrollContentPresenter)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is ItemsPresenter)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
// to delete
dependencyObjectChildrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
// to delete
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is DataGridRowsPresenter)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
// to delete
dependencyObjectChildrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
// to delete
// to delete
// These are variation that I tried to determine what was causing/allowing the children count to go from 0 to the correct number
//MessageBox.Show("isFound: " + isFound + "\n" + dependencyObject.ToString() + "\nrow: " + row + "\nRowPresenterCount: " + VisualTreeHelper.GetChildrenCount(dependencyObject));
MessageBox.Show("");
//dependencyObject.ToString();
//VisualTreeHelper.GetChildrenCount(dependencyObject);
// to delete
// to delete
dependencyObjectChildrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
// to delete
if (isFound)
{
isFound = false;
if (VisualTreeHelper.GetChildrenCount(dependencyObject) > row)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, row);
if (dependencyObject is DataGridRow)
{
returnList.Add(dependencyObject);
isFound = true;
}
}
}
// to delete
dependencyObjectChildrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
// to delete
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is Border)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is SelectiveScrollingGrid)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is DataGridCellsPresenter)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is ItemsPresenter)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is DataGridCellsPanel)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
isFound = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject) && !isFound; i++)
{
if (VisualTreeHelper.GetChild(dependencyObject, i) is DataGridCell)
{
dependencyObject = VisualTreeHelper.GetChild(dependencyObject, i);
isFound = true;
}
}
}
if (isFound)
{
dependencyObject = Get_DataGrid_Sibling_Cell((DataGridCell)dependencyObject, column) as DependencyObject;
if (dependencyObject != null)
{
returnList.Add(dependencyObject);
}
}
return returnList;
} // end Get_DataGridCell_By_Index()
我也试过这个来获取行,但我得到了一个空结果
DataGridRow row = vasgCategoryDataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;