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

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. 修饰符的范围+运算符优先级+构造方法特点+switch参数

    一.修饰符的范围 修饰符的范围,是否可访问: 类型 private 无修饰 protected public 同一类 是 是 是 是 同一包中的子类 否 是 是 是 同一包中的非子类 否 是 是 是 ...

  2. Codeforces 546D Soldier and Number Game(数论)

    类似筛素数的方法……求出前缀和.然后直接O(1)回答即可. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) ...

  3. codevs——2841 愤怒的LJF(背包)

    样例有误!  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description LJF发现ZPC的积分比他高,他很愤怒. 他挤出T ...

  4. [BZOJ3212][POJ3468]A Simple Problem with Integers

    题目大意: 维护一个长度为$n(n\leq100000)$的数列,支持区间加.区间求和两种操作,操作共$m(m\leq100000)$次. 思路: Splay区间操作. #include<cst ...

  5. Mac OS X上使用Wireshark抓包

    Wireshark针对UNIX Like系统的GUI发行版界面采用的是X Window(1987年更改X版本到X11).Mac OS X在Mountain Lion之后放弃X11,取而代之的是开源的X ...

  6. 3 Suggested Oracle Certifications For Oracle Form's Developers

    The following are the most suggested Oracle Certifications for Oracle Application Developers in Form ...

  7. C# DBHelper类

    class DBHelper { //public static string connstr = "server=.;database=SuperKTV;uid=sa;pwd=123456 ...

  8. 【Java】List转化为数组

    List转化为数组的两种方式: 第一种: List<String> list = new ArrayList<>(); String [] arr = list.toArray ...

  9. Android 获取虚拟按键的高度

    //获取虚拟按键的高度 public static int getNavigationBarHeight(Context context) { int result = 0; if (hasNavBa ...

  10. LeakCanary: 让内存泄露无所遁形

    LeakCanary: 让内存泄露无所遁形 09 May 2015 本文为LeakCanary: Detect all memory leaks!的翻译.原文在: https://corner.squ ...