# Command Binding

## Content

Command binding allows [InputPanel](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.html) controls to execute application logic through the `ICommand` interface instead of direct UI event handlers.
This approach separates UI interaction from application logic and supports command-driven application architectures such as MVVM. Using commands instead of control-specific event handlers improves maintainability, simplifies testing, centralizes command execution logic, and allows the same command implementation to be reused across multiple controls.
`InputPanel` controls can invoke commands automatically in response to user interaction events such as button clicks, value changes, or text updates.
**Supported Properties**
Supported `InputPanel` controls expose the following command-related properties.

| Property | Description |
| -------- | ----------- |
| `Command` | Specifies the `ICommand` instance executed when the control trigger event occurs |
| `CommandParameter` | Optional value passed to the command during execution |

**Command**
The [Command](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputHtmlLabel.Command.html) property binds the control to an `ICommand` implementation. The command executes automatically when the associated control interaction occurs.
**CommandParameter**
[CommandParameter](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputButton.CommandParameter.html) allows applications to pass contextual information to the command during execution. This is commonly used to identify the source control, provide operation context, pass metadata, or reuse a shared command across multiple controls.

***

## Supported Controls and Trigger Events

Each supported `InputPanel` control invokes its bound command through a specific interaction event.

| Control | Trigger Event | Behavior |
| ------- | ------------- | -------- |
| [InputButton](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputButton.html) | `OnClick` | Executes when the button is clicked |
| [InputCheckBox](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputCheckBox.html) | `OnCheckedChanged` | Executes when the checked state changes |
| [InputComboBox](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputComboBox.html) | `OnDropDownClosed` | Executes after the drop-down closes |
| [InputDatePicker](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputDatePicker.html) | `OnDropDownClosed` | Executes after the date picker closes |
| [InputHtmlLabel](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputHtmlLabel.html) | `LinkClicked` | Executes when a hyperlink is clicked |
| [InputMaskedTextBox](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputMaskedTextBox.html) | `OnTextChanged` | Executes when the text changes |
| [InputMenu](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputMenu.html) | `OnDropDownClosed` | Executes after the menu closes |
| [InputNumericBox](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputNumericBox.html) | `OnValueChanged` | Executes when the value changes |
| [InputRadioButton](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputRadioButton.html) | `OnCheckedChanged` | Executes when the selected state changes |
| [InputSplitButton](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputSplitButton.html) | `OnDropDownClosed` | Executes after the split-button menu closes |
| [InputTextBox](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputTextBox.html) | `OnTextChanged` | Executes when the text changes |
| [InputTimePicker](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputTimePicker.html) | `OnValueChanged` | Executes when the selected time changes |
| [InputTrackBar](/componentone/api/win/online-inputpanel/dotnet-api/C1.Win.InputPanel.10/C1.Win.InputPanel.InputTrackBar.html) | `OnValueChanged` | Executes when the slider value changes |

***

## Bind a Command to an `InputPanel` Control

The following example demonstrates how to bind a command to an `InputCheckBox`.
**Prerequisites**
Include the required namespaces.

```csharp
using System;using System.Windows.Forms;
using System.Windows.Input;
```

### Step 1: Create a Command

Create a class that implements `ICommand`.

```csharp
public class TestCommand : ICommand
{
    public bool CanExecuteValue { get; set; } = true;

    public bool WasExecuted { get; private set; } = false;

    public object PassedParameter { get; private set; } = null;

    public bool CanExecute(object parameter)
    {
        PassedParameter = parameter;
        return CanExecuteValue;
    }

    public void Execute(object parameter)
    {
        PassedParameter = parameter;
        WasExecuted = true;

        MessageBox.Show("Command executed: " + parameter.ToString());
    }

    public event EventHandler CanExecuteChanged;

    public void RaiseCanExecuteChanged() =>
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
```

The command implementation uses `CanExecute` to determine command availability, `Execute` to perform the command logic, and `CanExecuteChanged` to notify the UI when command availability changes.

### Step 2: Create and Initialize the Command

Create an instance of the command.

```csharp
private readonly TestCommand _command = new TestCommand
{
    CanExecuteValue = true
};
```

### Step 3: Bind the Command

Assign the command and optional parameter to the control.

```csharp
inputCheckBox.Command = _command;
inputCheckBox.CommandParameter = "CheckBox Toggled";
```

### Step 4: Run the Application

When the checkbox state changes:

* `CanExecute` is evaluated.
* If `CanExecute` returns `true`, the command executes.
* The control automatically updates its `Enabled` state.

***

## **Automatic Enabled State Management**

`InputPanel` controls automatically synchronize their `Enabled` state using the result of the command `CanExecute` method.
Example:

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

When `CanExecute` returns `false`, the control becomes disabled and command execution does not occur.
This behavior allows command logic to control UI availability without requiring manual state updates.

***

## Limitations

| Limitation | Description |
| ---------- | ----------- |
| Enabled-state synchronization only | Only the `Enabled` property is updated automatically |
| No automatic visual-state management | Custom styling and visual-state changes are not managed automatically |
| Frequent execution for some controls | Text and slider controls may trigger commands repeatedly |
| Link metadata handling | `InputHtmlLabel` does not automatically pass the clicked URL as the command parameter |
