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 ...
随机推荐
- UVALive 3517:Feel Good(单调栈 Grade C)
VJ题目链接 题意: n个数,求区间[l,r] 使得 sum[l,r]*min(a[l],a[l+1],...,a[r]) 最大.若有多种答案,输出区间最短的.若还有多组,输出最先出现的. 思路: 求 ...
- rsync 备份 CENTOS 系统!
rsync是一个非常优秀的文件同步工具,从它的名字可以看出,它支持远程同步.当然,在备份我的桌面系统时,只需要用到它的本地同步功能就行了.之所以选择rsync,是因为它具有如下优点:在备份还原过程中, ...
- getenv, _wgetenv
Description The C library function char *getenv(const char *name) searches for the environment strin ...
- 维护一套同时兼容 iOS 6 和 iOS 7,并且能够自动适应两个系统的 UI 风格的代码
举例:ios6:test.pngios7:ios7_test.png在ios7Image.plist中添加 "test" PS:如果要统一成ios7风格,可以看看UI7Kit yo ...
- xamarin.from ToolbarItem 字体大小和颜色更改
在xamarin.from 上我们经常会使用到页面跳转方式, new NavigationPage(newp page()){ BarBackgroundColor=Color.FromHex(&qu ...
- android windowSoftInputMode属性详解(转)
activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一]当有焦点产生时,软键盘是隐藏还是显示 [二]是否减少活动 ...
- cordova 中de.sitewaerts.cordova.documentviewer 插件 看pdf图片缩略图与实际图片不一致
//if (document == nil) // Unarchive failed so create a new ReaderDocument object //{ document = [[Re ...
- web.config配置数据库连接 【转】
http://www.cnblogs.com/breezeblew/archive/2008/05/01/1178719.html 第一种: 取连接字符串 = System.Web.Configura ...
- 转: maven打可执行的jar包以及classpath设置
from: http://drizzlewalk.blog.51cto.com/2203401/416508
- 基于Wiremock创建Mock Service平台
转载:http://blog.csdn.net/liuchunming033/article/details/52399397 1.Wiremock工具介绍 一般开发项目都会分模块进行,比如都会把前端 ...