在探索了 DataGrid 一段时间后,我发现我的问题没有任何意义。我上面的代码只准备了 DataGrid,但不填充任何数据。在此之前不会生成任何行,因此找不到任何行underlying TextBox controls
。
当 DataGrid 最终被数据填充时,获取底层控件的最佳方法似乎是捕获LoadinRow
事件。但是当这个事件触发时,行的加载还没有完成。需要临时分配第二个事件,该事件在最终加载行时触发。
{
DataGrid.LoadingRow += DataGrid_LoadingRow;
}
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
// The visual tree is not built until the row is "loaded". This event fires when this happend:
e.Row.Loaded += DataGrid_Row_Loaded;
}
private void DataGrid_Row_Loaded(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = (DataGridRow)sender;
// important: Remove the event again
dataGridRow.Loaded -= DataGrid_Row_Loaded;
NestedGridFieldProperty ngrProp = (NestedGridFieldProperty)dataGridRow.Item;
// Get the "presenter", which contains the cells
DataGridCellsPresenter presenter = coNeboTools.ConeboMisc.GetVisualChild<DataGridCellsPresenter>(dataGridRow);
// Get the cells in the presenter
var cells = GetVisualChildren<DataGridCell>(presenter);
// Get the underlying TextBox in column 0
TextBox underlyingTextBox = (TextBox)cells.ElementAt(0).Content;
// the Item property of the row contains the row data
var myData = dataGridRow.Item;
// do what ever is needed with the TextBlock
underlyingTextBox.Foreground = Brushes.Red;
}
// Static helper method to handle the visual tree
public static IEnumerable<T> GetVisualChildren<T>(DependencyObject dependencyObject)
where T : DependencyObject
{
if (dependencyObject != null)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in GetVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
// Static helper method to handle the visual tree
public static childItem GetVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
foreach (childItem child in GetVisualChildren<childItem>(obj))
{
return child;
}
return null;
}