Back to Skills

binding-enum-command-parameters

verified

Binds enum values to WPF CommandParameter using x:Static markup extension. Use when passing enum types to commands or avoiding string-based command parameter errors.

View on GitHub

Marketplace

dotnet-claude-plugins

christian289/dotnet-with-claudecode

Plugin

wpf-dev-pack

development

Repository

christian289/dotnet-with-claudecode
5stars

wpf-dev-pack/skills/binding-enum-command-parameters/SKILL.md

Last Verified

January 23, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/binding-enum-command-parameters/SKILL.md -a claude-code --skill binding-enum-command-parameters

Installation paths:

Claude
.claude/skills/binding-enum-command-parameters/
Powered by add-skill CLI

Instructions

# WPF Command Parameter Enum Type Binding

## Problem Scenario

When binding enum values to `CommandParameter` in WPF, **passing as string causes type mismatch error**.

### Error Message
```
System.ArgumentException: 'Parameter "parameter" (object) cannot be of type System.String,
as the command type requires an argument of type MyNamespace.MyEnum.'
```

### Cause
When specifying `CommandParameter="Pan"` as a string in XAML, WPF passes it as `System.String` type. However, if the Command expects a specific enum type, automatic type conversion does not occur.

---

## Solution

### Use `x:Static` to Directly Reference Enum Value

```xml
<!-- Namespace declaration -->
xmlns:viewmodels="clr-namespace:MyApp.ViewModels;assembly=MyApp.ViewModels"

<!-- Wrong method (passed as String) -->
<Button Command="{Binding SelectToolCommand}"
        CommandParameter="Pan" />

<!-- Correct method (passed as Enum type) -->
<Button Command="{Binding SelectToolCommand}"
        CommandParameter="{x:Static viewmodels:ViewerTool.Pan}" />
```

---

## Complete Example

### ViewModel (C#)
```csharp
public enum ViewerTool
{
    None,
    Pan,
    Zoom,
    WindowLevel
}

public partial class ViewerViewModel : ObservableObject
{
    [ObservableProperty] private ViewerTool _currentTool = ViewerTool.Pan;

    [RelayCommand]
    private void SelectTool(ViewerTool tool)
    {
        CurrentTool = tool;
    }
}
```

### View (XAML)
```xml
<UserControl xmlns:viewmodels="clr-namespace:MyApp.ViewModels;assembly=MyApp.ViewModels">

    <StackPanel Orientation="Horizontal">
        <!-- Select Pan tool -->
        <ToggleButton Content="Pan"
                      Command="{Binding SelectToolCommand}"
                      CommandParameter="{x:Static viewmodels:ViewerTool.Pan}"
                      IsChecked="{Binding CurrentTool,
                                 Converter={StaticResource EnumToBoolConverter},
                                 ConverterParameter={x:Static viewmodels:ViewerTool.Pan}}"

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
3151 chars