WPF: Binding to commands in code behind
Asked Answered
B

4

15

I have a WPF Microsoft Surface Application 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?

Batsheva answered 17/6, 2010 at 8:9 Comment(0)
D
25

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;
Dumortierite answered 17/6, 2010 at 8:14 Comment(3)
+1 for correct answer. Also if needed one can use Binding in code (msdn.microsoft.com/en-us/magazine/cc700358.aspx#id0190003)Athlete
Thank you for the quick answer. Didn't think that it would be THAT easy ;)Batsheva
The Binding might be set in the code behind but the implementation should NOT be and this code would only work if it was.Forbid
U
25

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"));
Underproduction answered 14/10, 2014 at 22:11 Comment(1)
item2.SetBinding(MenuItem.CommandProperty, new Binding("LogoutCommand", source: BindingContext)); I'm not sure why but my case only works if I specifically define the sourceAllineallis
C
6

I took the code from the link posted by Anvaka as template. I use RadMenuItem of Telerik, but surely you can use any other component that expose Command property.

item = new RadMenuItem();
item.Header = "Hide Column";
DependencyProperty commProp = RadMenuItem.CommandProperty;
if (!BindingOperations.IsDataBound(item, commProp)) {
  Binding binding = new Binding("HideColumnCommand");
  BindingOperations.SetBinding(item, commProp, binding);
}
//this is optional, i found easier to pass the direct ref of the parameter instead of another binding (it would be a binding to ElementName).
item.CommandParameter = headerlCell.Column;
menu.Items.Add(item);

Hope it helps ... and if something is not clear, sorry, it's my first post :)

Circuity answered 12/9, 2012 at 9:32 Comment(0)
P
1

This works

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Path=SaveReservationCommand}"
Prostatitis answered 14/5, 2019 at 10:30 Comment(1)
You are right. I don't know why I answered that way:)Prostatitis

© 2022 - 2024 — McMap. All rights reserved.