DataGridTemplateColumn 如何获取内部控件

10 篇文章 0 订阅

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值