DataGridTemplateColumn 如何获取内部控件
2013-09-04 17:52
309 查看
WPF中有时候我们不使用DataGridTextColumn 而使用用途更加宽广的DataGridTemplateColumn
但是用途多的东西当然也更复杂。
这里说下如何取DataGridTempateColumn得内部控件
一般可以用以下代码:
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 所属元素的上层去取,查看以下代码:
在实际使用中, 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.
连接:http://social.msdn.microsoft.com/Forums/silverlight/en-US/00c2473f-e4f2-4bef-9123-a433c92d0111/visualtreehelper-and-datagridtemplatecolumn
相关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元素的解析类
最后顺便说一句,DataGridTemplateColumn 要想和DataGridTextColumn排序,请这么做:
http://stackoverflow.com/questions/2375237/wpf-system-argumentexception-0-is-not-a-visual-or-visual3d
但是用途多的东西当然也更复杂。
这里说下如何取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.
连接:http://social.msdn.microsoft.com/Forums/silverlight/en-US/00c2473f-e4f2-4bef-9123-a433c92d0111/visualtreehelper-and-datagridtemplatecolumn
相关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>
http://stackoverflow.com/questions/2375237/wpf-system-argumentexception-0-is-not-a-visual-or-visual3d
相关文章推荐
- WPF DataGridTemplateColumn 获取内部控件
- [VB.NET]急,如何用js获得,设置datagrid 的template column里面放的控件值?
- 在WPF中获取DataGridTemplateColumn模板定义的内容控件
- 在WPF中获取DataGridTemplateColumn模板定义的内容控件
- 如何在后台代码获取控制EditItemTemplate的控件
- DataGrid 通过内部控件获取所在控件的列索引
- DataGrid的DataGridColumn内部如何调用外部method
- Windows Phone 如何获取ListBox的ItemTemplate/DataTemplate 中指定的控件
- NET2005中如何获取DataGrid模板里TextBox的值(<ItemTemplate>)
- 如何获取GridView的EmptyDataTemplate中的控件
- wpf——datagrid中如何获取DataGridTextColumn等某行某列的值
- WPF DataGrid DataGridTemplateColumn 列头checkbox如何在代码中取消选择
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- 如何使用asp:DataGrid中的HeaderTemplate中的控件
- 如何在ASP.NET里用DataView来达到DataGrid控件里的Column Sorting效果。
- EasyUI - datagrid中单元格里编辑控件的单击事件如何获取当前行的index
- C#中如何获取ItemsControl的DataTemplate中定义的控件?
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- Android如何在onCreate()方法中获取控件的高度和宽度
- 4.如何获取动态生成的SL控件的NAME值(二)