您的位置:首页 > 其它

DataGridTemplateColumn 如何获取内部控件

2013-09-04 17:52 309 查看
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.

连接: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: