Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF: Programmatically click the default button

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
5 Mar 2014CPOL 12.5K   4  
Invoke Button.Click event and associated ICommand on the current default button.

A one liner:

C#
System.Windows.Input.AccessKeyManager.ProcessKey(null, "\x000D", false); 

Background

A WPF Button with IsDefault set true registers itself with the AccessKeyManager, specifying the carriage return char (0x000d) as access key.

So AccessKeyManager.ProcessKey() will invoke the default button's ButtonBase.OnAccessKey(), which raises Click event and executes any associated command.
No need to loop controls to find the current default button.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --