binding-enum-command-parameters
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
<!-- 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#)
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)
<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}}" />
<!-- Select Zoom tool -->
<ToggleButton Content="Zoom"
Command="{Binding SelectToolCommand}"
CommandParameter="{x:Static viewmodels:ViewerTool.Zoom}"
IsChecked="{Binding CurrentTool,
Converter={StaticResource EnumToBoolConverter},
ConverterParameter={x:Static viewmodels:ViewerTool.Zoom}}" />
<!-- Select Window/Level tool -->
<ToggleButton Content="W/L"
Command="{Binding SelectToolCommand}"
CommandParameter="{x:Static viewmodels:ViewerTool.WindowLevel}"
IsChecked="{Binding CurrentTool,
Converter={StaticResource EnumToBoolConverter},
ConverterParameter={x:Static viewmodels:ViewerTool.WindowLevel}}" />
</StackPanel>
</UserControl>
Important Notes
- Namespace declaration required: Must declare the assembly and namespace where enum is defined in XAML
- Assembly reference: Specify
assembly=when using enum from different project - Also applies to Converter: Use
x:StaticforConverterParameteras well
Related Pattern
EnumToBoolConverter (for checking selection state)
public class EnumToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter) ?? false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? parameter : Binding.DoNothing;
}
}
More from christian289/dotnet-with-claudecode
using-avalonia-collectionview
Provides CollectionView alternatives for AvaloniaUI using DataGridCollectionView and ReactiveUI. Use when filtering, sorting, or grouping collections in AvaloniaUI applications.
9designing-avalonia-customcontrol-architecture
Defines the basic solution structure for AvaloniaUI Desktop Applications using CustomControl. Use when creating new AvaloniaUI projects or designing stand-alone control styles with ControlTheme.
9using-xaml-property-element-syntax
Converts long inline XAML bindings to Property Element Syntax for better readability. Use when XAML binding expressions become too long or complex.
8managing-styles-resourcedictionary
Manages WPF Style definitions and ResourceDictionary patterns including BasedOn inheritance and resource merging. Use when building theme systems, organizing resources, or choosing between StaticResource and DynamicResource.
8configuring-avalonia-dependency-injection
Configures GenericHost and Dependency Injection in AvaloniaUI applications. Use when setting up DI container, registering services, or implementing IoC patterns in AvaloniaUI projects.
7designing-wpf-customcontrol-architecture
Designs stand-alone control styles using WPF CustomControl and ResourceDictionary. Use when creating reusable custom controls or organizing control themes in Generic.xaml.
6