Setting Prism Command to a control in code

Topics: Prism v4 - Silverlight 4
Feb 16, 2011 at 10:50 AM

Hi,

Our application has a static menu (stackpanel + buttons) and we are migrating to a dynamic menu where buttons are added dynamically in code.

We are trying to replace this Xaml :
<Button x:Name="btConges" Style="{StaticResource MenuConges_Button}" cal:Click.Command="{Binding MenuItemClick}" cal:Click.CommandParameter="Conges" />


With :
StackPanel menuButton = (StackPanel) View.FindName("MenuButton");
Style txtCongesStyle = (Style)Application.Current.Resources["Menu_TextBlock"];

Button btConges = new Button();
if (btnCongesStyle != null) btConges.Style = btnCongesStyle;

menuButton.Children.Add(btConges);


But we have a problem with cal:Click.Command="{Binding MenuItemClick}" cal:Click.CommandParameter="Conges".
Commands are defined in a ViewModel, and we have a factory to marry Views and ViewModels.

How can we achieve that by code ?

Thanks,

Chris

Feb 16, 2011 at 1:50 PM

In VS2010 when you run the debugger are you seeing any errors related to missing bindings for your MenuItemClick property, in the output window at runtime?

Feb 16, 2011 at 1:58 PM

Thank you for the answer.

In fact our xaml is good.

What we want is to transfer the xaml <Button x:Name="btConges" Style="{StaticResource MenuConges_Button}" cal:Click.Command="{Binding MenuItemClick}" cal:Click.CommandParameter="Conges" /> in code.

And we don't know how to attach the the command MenuItemClick (defined in our ViewModel) and its parameter to our newly created button by code.

 

Thank you.

 

Christian