public void GetCell()
{
for (int i = 0; i < this.datagrid1.Items.Count; i++)
{
DataRowView drv = datagrid1.Items[i] as DataRowView;
DataGridRow row = (DataGridRow)this.datagrid1.ItemContainerGenerator.ContainerFromIndex(i);
if (i == 1)//如果行号等于1
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(1);//获取列号为1
cell.Background = new SolidColorBrush(Colors.Red);
Tooltip tip=new Tooltip();
tip.context="1234";
cell.Tooltip=tip;
}
}
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T childContent = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
childContent = v as T;
if (childContent == null)
{
childContent = GetVisualChild<T>(v);
}
if (childContent != null)
{
break;
}
}
return childContent;
}
//防止在取datagrid的row时出现错误,在Window_Loaded事件里面调用
public void Loed()
{
if (!Window.GetWindow(dataGrid1).IsVisible)
{
Window.GetWindow(dataGrid1).Show();
}
dataGrid1.UpdateLayout();
}