Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind a Command in WPF

Sometimes we used complex ways so many times, we forgot the simplest ways to do the task.

I know how to do command binding, but i always use same approach.

Create a class that implements ICommand interface and from the view model i create new instance of that class and binding works like a charm.

This is the code that i used for command binding

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;            
        testCommand = new MeCommand(processor);
    }

    ICommand testCommand;

    public ICommand test
    {
        get { return testCommand; }
    }
    public void processor()
    {
        MessageBox.Show("hello world");
    }
}

public class MeCommand : ICommand
{
    public delegate void ExecuteMethod();
    private ExecuteMethod meth;
    public MeCommand(ExecuteMethod exec)
    {
        meth = exec;
    }

    public bool CanExecute(object parameter)
    {
        return false;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        meth();
    }
}

But i want to know the basic way to do this, no third party dll no new class creation. Do this simple command binding using a single class. Actual class implements from ICommand interface and do the work.

like image 994
Manvinder Avatar asked Dec 12 '12 11:12

Manvinder


People also ask

What is command binding in WPF?

The command is the action to be executed. The command source is the object which invokes the command. The command target is the object that the command is being executed on. The command binding is the object which maps the command logic to the command.

How do you bind a code behind command?

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.

What is a binding command?

bind command is Bash shell builtin command. It is used to set Readline key bindings and variables. The keybindings are the keyboard actions that are bound to a function. So it can be used to change how the bash will react to keys or combinations of keys, being pressed on the keyboard.

What is the syntax for binding a command to an object?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.


1 Answers

Prism already provided Microsoft.Practices.Prism.Commands.DelegateCommand

I'm not sure is it considered as 3rd party. At least it's official and documented on MSDN.

Some native build-in commands such copy, paste implements ICommand interface. IMHO it following the Open(for extends)/Close(for changes) principle. so that we can implement our own command.


Update

As WPF Commanding documented here, an excerpt...

WPF provides a set of predefined commands. such as Cut, BrowseBack and BrowseForward, Play, Stop, and Pause.

If the commands in the command library classes do not meet your needs, then you can create your own commands. There are two ways to create a custom command. The first is to start from the ground up and implement the ICommand interface. The other way, and the more common approach, is to create a RoutedCommand or a RoutedUICommand.

I've tried RoutedCommand model at the beginning and ended up with implementing ICommand.

sample XAML binding

<CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />

RoutedCommand is not different from RoutedEvent. this seems like a better button's 'Clicked' event handler. It serves the purpose: To separate app logic from View but require some attach DependencyProperty or code-behind.

personally I feel more comfortable with just implement my ICommand.

like image 82
aifarfa Avatar answered Sep 19 '22 12:09

aifarfa