模拟点击datagrid标题列 [英] Simulate a click on datagrid header column

查看:166
本文介绍了模拟点击datagrid标题列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找并发现:如何模拟用户点击WPF中的datagrid的列标题,但是我不知道我必须放在your_control中,我使用的是Silverlight 5可以有人帮我吗?

  DataGridColumnHeaderItemAutomationPeer peer = new DataGridColumnHeaderItemAutomationPeer(Your_control); 

我的DataGrid是dgEmployee



当我尝试使用它,定义它:

  DataGridColumnHeaderItemAutomationPeer peer = new DataGridColumnHeaderItemAutomationPeer(dgEmployee); 

系统发送错误:



与System.Windows.Automation.Peers.DataGridColumnHeaderAutomationPeer.DataGridColu mnHeaderAutomationPeer(System.Windows.Controls.Primitives.DataGridColumnHeader)的最佳重载方法匹配有一些无效的参数



如何将dgEmployee的DataGridColumnHeader作为参数?



谢谢!!

解决方案

解决方案是:

  System.Windows.Controls.Primitives .DataGridColumnHeader headerObj; 

headerObj = GetColumnHeaderFromColumn(myDataGrid,myDataGrid.Columns [1] .Header);

System.Windows.Automation.Peers.DataGridColumnHeaderAutomationPeer peer =
new DataGridColumnHeaderAutomationPeer(headerObj);
IInvokeProvider invoker =(IInvokeProvider)peer;
invoker.Invoke(); //以编程方式调用点击


private System.Windows.Controls.Primitives.DataGridColumnHeader GetColumnHeaderFromColumn(DependencyObject parent,object header)
{
int count = VisualTreeHelper。 GetChildrenCount(parent); (int i = 0; i< count; i ++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent,i);
if(child!= null)
{
if(child is System.Windows.Controls.Primitives.DataGridColumnHeader)
{
System.Windows.Controls.Primitives .DataGridColumnHeader columnHeader = child as System.Windows.Controls.Primitives.DataGridColumnHeader;
if(header.Equals(columnHeader.Content))
{
return columnHeader;
}
}
else
{
System.Windows.Controls.Primitives.DataGridColumnHeader columnHeader = GetColumnHeaderFromColumn(child,header);
if(null!= columnHeader)
{
return columnHeader;
}
}
}
}

返回null;

}

可能对某人有帮助, p>

I was looking for and found: How to simulate the user click on a column header of a datagrid in WPF?, but I don't know what I must put in "your_control", I'm using Silverlight 5, can somebody help me??

DataGridColumnHeaderItemAutomationPeer peer = new DataGridColumnHeaderItemAutomationPeer (Your_control);

my DataGrid is dgEmployee

When I try use it, defining it so:

DataGridColumnHeaderItemAutomationPeer peer = new DataGridColumnHeaderItemAutomationPeer (dgEmployee);

System send me an error:

"The best overloaded method match for 'System.Windows.Automation.Peers.DataGridColumnHeaderAutomationPeer.DataGridColu‌​mnHeaderAutomationPeer(System.Windows.Controls.Primitives.DataGridColumnHeader)' has some invalid arguments"

How I can put the DataGridColumnHeader of my dgEmployee as argument?

Thanks!!

解决方案

the solution is:

System.Windows.Controls.Primitives.DataGridColumnHeader headerObj;

headerObj = GetColumnHeaderFromColumn(myDataGrid, myDataGrid.Columns[1].Header);

System.Windows.Automation.Peers.DataGridColumnHeaderAutomationPeer peer =
                                new DataGridColumnHeaderAutomationPeer(headerObj);
                            IInvokeProvider invoker = (IInvokeProvider)peer;
                            invoker.Invoke(); // Invoke a click programmatically   


    private System.Windows.Controls.Primitives.DataGridColumnHeader GetColumnHeaderFromColumn(DependencyObject parent, object header)
    {            
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child != null)
            {
                if (child is System.Windows.Controls.Primitives.DataGridColumnHeader)
                {
                    System.Windows.Controls.Primitives.DataGridColumnHeader columnHeader = child as System.Windows.Controls.Primitives.DataGridColumnHeader;
                    if (header.Equals(columnHeader.Content))
                    {
                        return columnHeader;
                    }
                }
                else
                {
                    System.Windows.Controls.Primitives.DataGridColumnHeader columnHeader = GetColumnHeaderFromColumn(child, header);
                    if (null != columnHeader)
                    {
                        return columnHeader;
                    }
                }
            }
        }

        return null;

    }

maybe can be helpful for somebody, regards from Mexico

这篇关于模拟点击datagrid标题列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆