WPF: Binding to commands in code behind

Assuming that you have a named your SurfaceButton to "SurfaceButton1" and you have access to an instance of the command, you can use the following code:

SurfaceButton1.Command = SaveReservationCommand;

The accepted answer will work great if the Button has access to the Command. However, in MVVM these are usually kept separate (the Button in the View and the Command in the View-Model). In XAML you'd normally use a data binding to hook it up (like the example in the question).

My program gave me an error when my dynamic Button couldn't find the Command (because it was in a totally different namespace). This is how I ended up solving this:

SurfaceButton.SetBinding (Button.CommandProperty, new Binding("SaveReservationCommand"));