How to simulate the user click on a column header

2019-06-04 23:25发布

问题:

I need to simulate thru code the user clicking on a header of a WPF datagrid column (using .Net 4 in case it matters).

Is this possible?

回答1:

I think you can use the AutomationPeer classes, which are designed for this sort of thing, specifically the DataGridColumnHeaderItemAutomationPeer Class.

 DataGridColumnHeaderItemAutomationPeer peer = new DataGridColumnHeaderItemAutomationPeer (Your_control);
 IInvokeProvider invoker= (IInvokeProvider)peer;
 invoker.Invoke(); // Invoke a click programmatically