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

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"

wpf 获取datagrid 模板列中的控件的更多相关文章

  1. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  2. 关于如何获取Google 官方 NavgationView中的控件的方法

    最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...

  3. 在WPF中获取DataGridTemplateColumn模板定义的内容控件

    xaml格式描述: <DataGrid Name="dataGrid" Grid.Row="1" ItemsSource="{Binding}& ...

  4. 在WPF中获取DATAGRIDTEMPLATECOLUMN模板定义的内容控件(转载)

    原文:http://www.cnblogs.com/eric_ibm/p/3772516.html xaml格式描述: <DataGrid Name="dataGrid" G ...

  5. WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件

    在之前的DataGrid的DataGridTemplateColumn列中,自定义了一个TextBox控件,但是在C#代码中提示找不到这个控件,导致无法对该控件进行操作.在网上搜索后,发现一些处理方法 ...

  6. js获取gridview模板列中textbox行列的值

    下面一个例子:在gridview中第一列输入数值,第二列输入数值,点击第三列的时候进行计算 求和,如果不符合标记为红色字体. 如图: 代码 : <html xmlns="http:// ...

  7. GridControl 控制列中的控件显示

    一.需求描述 根据条件判断Checkbox的显示,先上个图,, 类似Demo中的这个,因为不能控制文本的显示,所以需对该列的其它事件做些处理,  二.解决方案 1.添加GridControl上需要的控 ...

  8. ASP.NET中获取Repeater模板列中LinkButton按钮事件中获取ID等

    前台页面中: <asp:Repeater ID="repComment" runat="server">            <ItemTe ...

  9. WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问

    原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem ...

随机推荐

  1. linux 通过MD5监控指定路径文件的变动

    脚本须知: 1. 运行此脚本的用户必须是root,因为在某些文件所在路径普通用户没有访问权限 2. 源文件和其md5码只要有一方内容有改动,都会导致校验失败,所以校验码的保存就至关重要防止其他人修改, ...

  2. django+nginx+mod_wsgi+apache网站部署

    软件安装 0.如果对selunix不熟就先把它停掉 vi /etc/selinux/config 改动selinux=diabled 然后重启计算机 1.编译安装python mod_wsgi的安装需 ...

  3. 解决WCF部署到IIS出现“证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限”

    访问WCF服务时,出现异常详细信息: System.Security.Cryptography.CryptographicException: 密钥集不存在.ArgumentException: 证书 ...

  4. windows下安装oracle,sqlplus连接启动oracle(oracle 主机字符串输入是什么)

    1.oracle安装 参考http://wenku.baidu.com/view/d01ffd43336c1eb91a375d68.html,这里不再赘述 2.命令行sqlplus连接oracle   ...

  5. tomcat7.0.55配置HTTP强制跳转到HTTPS

    首先需要配置好HTTPS单向或双向链接 参考: tomcat7.0.55配置单向和双向HTTPS连接(二) 然后编辑tomcat的conf目录下的web.xml 在<welcome-file-l ...

  6. python安装在windows server2008,故障排除

    python安装在windows server2008,故障排除   我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...

  7. dubbo框架的简单介绍

    以下的官网的介绍. dubbo是SOA.小例子是简单的远程调用(生产者消费者的模式出现).http://blog.csdn.net/huangyekan/article/details/4217267 ...

  8. Nginx 初探

    Nginx简介 Nginx(engine X)是一个开源.轻量级.高性能的HTTP和反向代理服务器,可以代理HTTP. IMAP/POP3/SMTP和TCP/UDP协议:其特点是占用内存少,并发能力强 ...

  9. Cookie常用方法封装Utils

    1.查询某个指定的cookie package com.sun.etalk.cookie; import javax.servlet.http.Cookie; public class CookieU ...

  10. 转:css:Position

    http://www.cnblogs.com/polk6/archive/2013/07/26/3214847.html http://blog.sina.com.cn/s/blog_4bcf4a5e ...