Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Binding to commands in code behind

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?

like image 454
sofri Avatar asked Jun 17 '10 08:06

sofri


People also ask

How do you bind a code behind command?

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).

What is command binding in WPF?

C# Copy. void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } A CommandBinding is attached to a specific object, such as the root Window of the application or a control. The object that the CommandBinding is attached to defines the scope of the binding.

What is two way binding WPF?

Its very easy to do in WPF in comparison of windows Application programming. Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.

What is the use of ICommand in WPF?

ICommand is an interface between the Presentation & the BusinessLogic layer. Whenever any button is pressed on the screen, XAML has its code-behind ButtonClick event. But in MVVM architecture, there is no room for code-behind to keep the application loosely coupled, so ICommand was introduced.


2 Answers

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 :)

like image 57
Zu1779 Avatar answered Oct 04 '22 09:10

Zu1779


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"));
like image 45
Nightmare Games Avatar answered Oct 04 '22 08:10

Nightmare Games