问题
I come to write my first WPF Control. It contains a DataGrid filled with data. Change the font of some of the cells based by function that I call in the main form.Its here I really hit rock bottom.Anyone has ideas?
========================================================================= I Looked in the topic above and still can't get my head around how to make it function that is dynamically called from the control in the main form. Here is the Code I am working on so far.
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;
}
}
Don't said is the best idea since it's my very first experience with WPF.
来源:https://stackoverflow.com/questions/32584353/wpf-control-datagrid-cell-color-change