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 ...
随机推荐
- LeetCode OJ-- Jump Game II **
https://oj.leetcode.com/problems/jump-game-ii/ 给一个数列,每次可以跳相应位置上的步数,问跳到最后位置至少用几步. 动态规划: j[pos]表示从0到po ...
- (转)十步完全理解 SQL
十步完全理解 SQL 目录[-] 10个简单步骤,完全理解SQL 1. SQL 是一种声明式语言 2. SQL 的语法并不按照语法顺序执行 3. SQL 语言的核心是对表的引用(table refer ...
- 串口调试利器--Minicom配置及使用详解
因为现在电脑基本不配备串行接口,所以,usb转串口成为硬件调试时的必然选择.目前知道的,PL2303的驱动是有的,在dev下的名称是ttyUSB*. Minicom,是Linux下应用比较广泛的串口软 ...
- tcp/ip RFC
http://www.ietf.org/rfc.htmlhttp://www.rfc-editor.org
- Android6.0 新特性详解
一 运行时权限 Android6.0 引入了一个新的应用权限模型,期望对用户更容易理解,更易用和更安全.该模型将标记为危险的权限从安装时权限(Install Time Permission)模型 移动 ...
- Android常用URI收藏
转:http://www.android-study.com/jichuzhishi/338.html 以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一 ...
- span设置padding无效
<span style="display:inline-block;padding-top:10px">测试<span> 给span加属性 display: ...
- 2016.11.14 MIT challenge之课程总览
Degree Chartshttp://catalog.mit.edu/degree-charts/computer-science-engineering-course-6-3/ MIT Chall ...
- linux中nl用法
linux 中nl 命令使用 nl :添加行号打印 -b: 指定行号指定的方式,主要有两种: -b a : 表示不论是否为空行,都同样列出行号 -b t : 如果有空行,则不列出那一行 ...
- JSON之—— JSON.parse()和JSON.stringify() (插曲)
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46391269 parse用于从一个字符串中解析出json对象,如 var str ...