DataGridTemplateColumn 如何获取内部控件
WPF中有时候我们不使用DataGridTextColumn 而使用用途更加宽广的DataGridTemplateColumn
但是用途多的东西当然也更复杂。
这里说下如何取DataGridTempateColumn得内部控件
一般可以用以下代码:
private void DataGrid_MouseRightButtonUp(object sender,
MouseButtonEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null)
!(dep is DataGridCell)
!(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridColumnHeader)
{
DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
// do something
}
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// do something
}
}
http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html以上代码使用 VisualTreeHelper 检索 DependencyObject的 Parent
我们也可以反过来做:
VisualTreeHelper 有 GetChildrenCount() GetChild(..) 这两个函数获取内部的孩子。
但是很遗憾 DataGridTemplateColumn 不会存在于VisualTree中,(查询会有错误,说对象不是 Visual 也不是Visual3D)
也不存在与LogicalTree中,你可以尝试GetParent或者GetChild分别是Null和一个空的IEnumrable
相关内容:
http://stackoverflow.com/questions/2375237/wpf-system-argumentexception-0-is-not-a-visual-or-visual3d
实际上DataGridTemplateColumn 内部没有你想要的东西,你应当从 DataGridTemplateColumn 所属元素的上层去取,查看以下代码:
void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (sender.IsNull() || !(sender is DataGrid)) return; //var cell = GetCell(dataGrid, 0, 14); //var ttt = WPFItem.GetWPFItem(cell); if ( e.IsNotNull() && e.Device.IsNotNull() && e.Device.Target.IsNotNull() && e.Device.Target is DataGridColumnHeader) { var columnHelper = ((DataGridColumnHeader)e.Device.Target); var column = columnHelper.Column; } }
在实际使用中, columnHelper.Column 可能是普通的 DataGridTextColumn, 也可能是复杂的 DataGridTemplateColumn ,
我们应当从 columnHelper入手 , 使用 VisualTreeHelper.GetChildrenCount() GetChild() 这两个函数就能取到 DataGridTemplateColumn 内部的数据了。
当然你不可能在 columnHelper的 children 中发现DataGridTemplateColumn, 他不存在与VisualTree中。
http://stackoverflow.com/questions/2375237/wpf-system-argumentexception-0-is-not-a-visual-or-visual3d
一个人是这么说的:
When you look at the documentation you
can see that the VisualTreeHelper expects a UIElement, but a data grid column is only a dependency object, not a visual element.
相关VisualTree , LogicalTree的资料:
http://www.codeproject.com/Articles/21495/Understanding-the-Visual-Tree-and-Logical-Tree-in
http://msdn.microsoft.com/en-us/library/ms753391.aspx
最后贴一个枚举一个WPF元素的解析类
class WPFItem { public DependencyObject dpObject; public WPFItem[] Children; public static WPFItem GetWPFItem(DependencyObject arg_dpobject) { var item = new WPFItem(); item.dpObject = arg_dpobject; var nChildCount = VisualTreeHelper.GetChildrenCount(arg_dpobject); item.Children = new WPFItem[nChildCount]; for (var i = 0; i < nChildCount; i++) { item.Children[i] = GetWPFItem(VisualTreeHelper.GetChild(arg_dpobject, i)); } return item; } public override string ToString() { return string.Format("{0},[{1}]", dpObject.ToString2(), Children.IsEmpty() ? "0" : Children.Length.ToString()); } }
最后顺便说一句,DataGridTemplateColumn 要想和DataGridTextColumn排序,请这么做:
SortMemberPath="ImpRepo"要加上SortMemberPath就能排序了
<DataGridTemplateColumn Header="隐含回购利率(%)" MinWidth="70" IsReadOnly="True" SortMemberPath="ImpRepo"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ImpRepo, StringFormat={}{0:n2},UpdateSourceTrigger=PropertyChanged}" Foreground="{Binding Path=ImpRepo,Converter={StaticResource IRRColorConvert}}"/> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能