作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有交替行背景颜色的数据绑定(bind) DataGrid。我想根据单元格包含的数据对单元格进行不同的着色。我已经尝试过这个线程建议的解决方案
http://wpf.codeplex.com/Thread/View.aspx?ThreadId=51143
但,
DataGridCellsPresenter 演示者 = GetVisualChild(row)
总是返回 null。
我在用
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;
}
最佳答案
如果您知道要访问的单元格的行和索引,那么您可以在代码中执行以下操作:
//here's usage
var cell = myDataGrid.GetCell(row, columnIndex);
if(cell != null)
cell.Background = Brushes.Green;
public static class DataGridExtensions
{
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int columnIndex = 0)
{
if (row == null) return null;
var presenter = row.FindVisualChild<DataGridCellsPresenter>();
if (presenter == null) return null;
var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (cell != null) return cell;
// now try to bring into view and retreive the cell
grid.ScrollIntoView(row, grid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
return cell;
}
关于WPF - 如何从 DataGridRow 中获取单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3671003/
我是一名优秀的程序员,十分优秀!