wpf 获取datagrid 模板列中的控件

目前采用的 方法  (网上提供的一款)

public static DataGridRow GetRow(DataGrid datagrid, int columnIndex)
        {
            DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);
            if (row == null)
            {
                datagrid.UpdateLayout();
                datagrid.ScrollIntoView(datagrid.Items[columnIndex]);
                row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);  //确保这一行出现
            }
            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;
        }
        public static DataGridCell GetCell(DataGrid datagrid, int rowIndex, int columnIndex)
        {
            DataGridCell cell=new DataGridCell ();
            try
            {
                DataGridRow rowContainer = GetRow(datagrid, rowIndex);

if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

//cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    cell = CommonHelper.GetCell(datagrid, rowIndex, 8);
                    if (cell == null)
                    {
                        datagrid.ScrollIntoView(rowContainer, datagrid.Columns[columnIndex]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    }
                    return cell;
                }
            }
            catch (Exception ex)
            {
                
            }
            return cell;
        }

public static T FindVisualChildByName<T>(Visual parent, string name) where T : Visual
        {
            if (parent != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i) as Visual;
                    string controlName = child.GetValue(Control.NameProperty) as string;
                    if (controlName == name)
                    {
                        return child as T;
                    }
                    else
                    {
                        T result = FindVisualChildByName<T>(child, name);
                        if (result != null)
                            return result;
                    }
                }
            }
            return null;
        }

自己使用的一种方法

string strReportStatus = (dgReportList.Items[i] as DataRowView)["STATUS"].ToString();
                    DataGridTemplateColumn templeColumn = dgReportList.Columns[8] as DataGridTemplateColumn;

FrameworkElement s = dgReportList.Columns[8].GetCellContent(dgReportList.Items[i]);
                    TextBlock tbOper = templeColumn.CellTemplate.FindName("blockOper", s) as TextBlock;

当循环到一定的 行时   会报出为空 FrameworkElement s = dgReportList.Columns[8].GetCellContent(dgReportList.Items[i]);

solution:

你的datagrid是默认开启了ui virtualization 的吧,如果是这样的话,VisualTree并不是所有的控件,为了显示加速,virtualization默认的只会加载一定范围的控件,不显示的控件并不加载.

正解!!!!!!!!!!!!! 怒赞!!!!!!!!!!!!!!!!!!!

在datagrid属性里面要关闭就可以了,不过加时间是问题,如果不是分页的话

EnableColumnVirtualization="False"

上一篇:深入理解.NET程序的原理 谈一谈破解.NET软件的工具和方法


下一篇:C# wpf中DataGrid 支持汇总行