Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF: Programmatically click the default button

0.00/5 (No votes)
5 Mar 2014 1  
Invoke Button.Click event and associated ICommand on the current default button.

A one liner:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here