65.9K
CodeProject is changing. Read more.
Home

WPF: Programmatically click the default button

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Mar 5, 2014

CPOL
viewsIcon

13026

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.