我是 WPF 新手
我试图根据列和行索引更改单个单元格的边框。到目前为止,我已经有了获取列和行索引的代码。
现在我需要得到'那个单元格'并改变它的边界..
这是我的代码,但它不起作用:
我从网上得到了这个方法:
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);
else
break;
} return child;
}
然后这是我需要更改单元格属性的地方..
int rows = 0;
int col = 0;
while (col < myDG.Columns.Count)
{
rows = 0;
while (rows < myDG.Items.Count)
{
DataGridRow row = (DataGridRow)myDG.ItemContainerGenerator.ContainerFromIndex(rows);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col);
cell.BorderThickness = new Thickness (2,2,2,2);
cell.BorderBrush= Brushes.Black;
rows++;
}
col++;
}
任何想法?提前致谢