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"
wpf 获取datagrid 模板列中的控件的更多相关文章
- WPF 中获取DataGrid 模板列中控件的对像
WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...
- 关于如何获取Google 官方 NavgationView中的控件的方法
最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...
- 在WPF中获取DataGridTemplateColumn模板定义的内容控件
xaml格式描述: <DataGrid Name="dataGrid" Grid.Row="1" ItemsSource="{Binding}& ...
- 在WPF中获取DATAGRIDTEMPLATECOLUMN模板定义的内容控件(转载)
原文:http://www.cnblogs.com/eric_ibm/p/3772516.html xaml格式描述: <DataGrid Name="dataGrid" G ...
- WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件
在之前的DataGrid的DataGridTemplateColumn列中,自定义了一个TextBox控件,但是在C#代码中提示找不到这个控件,导致无法对该控件进行操作.在网上搜索后,发现一些处理方法 ...
- js获取gridview模板列中textbox行列的值
下面一个例子:在gridview中第一列输入数值,第二列输入数值,点击第三列的时候进行计算 求和,如果不符合标记为红色字体. 如图: 代码 : <html xmlns="http:// ...
- GridControl 控制列中的控件显示
一.需求描述 根据条件判断Checkbox的显示,先上个图,, 类似Demo中的这个,因为不能控制文本的显示,所以需对该列的其它事件做些处理, 二.解决方案 1.添加GridControl上需要的控 ...
- ASP.NET中获取Repeater模板列中LinkButton按钮事件中获取ID等
前台页面中: <asp:Repeater ID="repComment" runat="server"> <ItemTe ...
- WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问
原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem ...
随机推荐
- Bluedroid之GKI
1. 概述 GKI以库libbt-brcm_gki.a的形式提供给Bluedroid使用 该层是一个适配层,适配了OS相关的进程.内存相关的管理,还可以用于线程间传递消息 主要通过变量gki_cb(结 ...
- C#图解教程学习笔记——转换
一.什么是转换转换(conversion)是指接受一个类型的值并使用它作为另一个类型的等价值的过程.转换后的值应和源值是一样的,但其类型为目标类型. 二.隐式转换和显式转换(强制转换)1. 隐式转换( ...
- javascript实现复选框的全选全不选
通过复选框的id获取到复选框 元素 对复选框绑定点击事件 每个checkbox都设置相同的name checkOne 通过得到的元素获取checkbox的状态 当点击全选全不选checkbox时,检查 ...
- cobbler一键部署centos7.4(脚本)
执行脚本之前你需要做四件事 1. 关闭防火墙 2.关闭selinux 3.配置163或者阿里云的 yum源 4.上传centos7.4的镜像如下图 [root@cobbler ~]# cat auto ...
- Codeforces Gym101502 E.The Architect Omar-find()函数
E. The Architect Omar time limit per test 1.0 s memory limit per test 256 MB input standard input ...
- Java 8 Comparator: 列表排序
在本文中,我们将看到几个关于如何在Java 8中对List进行排序的示例. 1.按字母顺序排序字符串列表 List<String> cities = Arrays.asList( &quo ...
- 根据CPU核数合理设置线程池大小
一般来说池中总线程数是核心池线程数量两倍,只要确保当核心池有线程停止时,核心池外能有线程进入核心池即可. 我们所需要关心的主要是核心池线程的数量该如何设置. 自定义线程池代码 package com. ...
- SQLite中使用全文搜索FTS
SQLite中使用全文搜索FTS SQLite支持全文搜索.通过全文搜索功能,可以方便用户快速进行查找.在iOS中,GRDB.FMDB等SQLite框架均支持FTS技术,如FTS3.FTS4等.各 ...
- JSP介绍与语法-java之JSP学习第一天(非原创)
文章大纲 一.JSP 简介二.JSP 生命周期三.JSP 语法四.学习资料下载五.参考文章 一.JSP 简介 1. 什么是Java Server Pages? JSP全称Java Server P ...
- SecureCRT介绍、安装、使用(转)
http://blog.csdn.net/liang19890820/article/details/49701429 简介 SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简 ...