我来写第一个WPF控件。它包含一个填充数据的DataGrid。根据我在主窗体中调用的函数更改某些单元格的字体。在这里,我真的触底了。任何人都有想法?
=============================================== ========================== 我看了上面的主题,但仍然无法理解如何使它从主窗体中的控件动态调用它的功能。这是我目前正在制定的准则。
public void PaintCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
CalendarGridView.ScrollIntoView(rowContainer, CalendarGridView.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
// I want to the text in the cell in red
}
}
}
private DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)CalendarGridView.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
CalendarGridView.UpdateLayout();
CalendarGridView.ScrollIntoView(CalendarGridView.Items[index]);
row = (DataGridRow)CalendarGridView.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
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;
}
}
不要说是最好的主意,因为这是我第一次使用WPF。