views:

54

answers:

1

Hi,

I have an WPF Microsoft Surface Applikation and I'm using MVVM-Pattern.

I have some buttons that are created in code behind. And I would like to bind commands to them but I only know how that works in the XAML like this:

<Custom:SurfaceButton Command="{Binding SaveReservationCommandBinding, Mode=OneWay}"/> 

But I cannot do it like this because my buttons do not exist in the XAML, only in the code behind.

So how would a command binding like that works in code behind?

+5  A: 

Hi,

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;
decyclone
+1 for correct answer. Also if needed one can use Binding in code (http://msdn.microsoft.com/en-us/magazine/cc700358.aspx#id0190003)
Anvaka
Thank you for the quick answer. Didn't think that it would be THAT easy ;)
sofri