XAML
Enum
Command Binding
Data Binding
MVVM

Passing an enum value as command parameter from XAML

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Passing an enum as a command parameter from XAML is a good MVVM pattern because it keeps the view expressive without hardcoding magic strings or numbers. The main trick is getting XAML to reference the enum value correctly and making the command accept the expected type. Once that is set up, command routing remains clean and strongly typed.

Define the Enum in Code

Start with an enum that expresses the action clearly.

csharp
1public enum FilterMode
2{
3    All,
4    Active,
5    Completed
6}

Using an enum here is already better than passing "All" or "2" through the UI layer.

Reference the Enum Value in XAML

In WPF, the common approach is x:Static.

xml
1<Window
2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4    xmlns:local="clr-namespace:MyApp">
5
6    <StackPanel>
7        <Button Content="Show All"
8                Command="{Binding ChangeFilterCommand}"
9                CommandParameter="{x:Static local:FilterMode.All}" />
10
11        <Button Content="Show Active"
12                Command="{Binding ChangeFilterCommand}"
13                CommandParameter="{x:Static local:FilterMode.Active}" />
14    </StackPanel>
15</Window>

The local namespace must point to where the enum is declared.

Accept the Parameter in the ViewModel

Your command can receive the enum as an object and cast it safely, or use a generic command implementation.

csharp
1using System;
2using System.Windows.Input;
3
4public class MainViewModel
5{
6    public ICommand ChangeFilterCommand { get; }
7
8    public MainViewModel()
9    {
10        ChangeFilterCommand = new RelayCommand(OnChangeFilter);
11    }
12
13    private void OnChangeFilter(object? parameter)
14    {
15        if (parameter is FilterMode mode)
16        {
17            CurrentFilter = mode;
18        }
19    }
20
21    public FilterMode CurrentFilter { get; private set; }
22}

This keeps the view model free from string parsing logic.

Strongly Typed Relay Commands

If your MVVM toolkit supports generic commands, the code becomes cleaner.

csharp
1public ICommand ChangeFilterCommand { get; }
2
3public MainViewModel()
4{
5    ChangeFilterCommand = new RelayCommand<FilterMode>(mode =>
6    {
7        CurrentFilter = mode;
8    });
9}

That gives compile-time intent and removes manual casting inside the handler.

Useful Variation: Enum from Data Binding

Sometimes the enum value comes from bound item data rather than from a fixed XAML literal. In that case, you do not need x:Static; you bind the property normally and let the selected item's enum flow through as the parameter.

That is useful in lists, menus, and context actions.

Why This Helps MVVM

Passing enums from XAML keeps presentation choices declarative while leaving decision logic in the view model. The button chooses a mode, and the command reacts to that mode. That separation is cleaner than writing click handlers in code-behind for every variation of the same action.

Why Enums Are Better Than Strings Here

Enums improve:

  • readability
  • refactor safety
  • switch statements in the view model
  • testability

If you rename an enum member, the compiler helps you. If you pass string literals, mistakes survive until runtime.

Debugging Failed Parameters

If the command parameter is null or wrong, check these first:

  1. the XAML namespace mapping
  2. the enum visibility
  3. whether the command expects the right type

Most failures are namespace or casting issues, not command-binding issues.

Common Pitfalls

  • Forgetting to declare the correct XAML namespace for the enum type.
  • Passing strings instead of enums and reintroducing parsing logic in the view model.
  • Receiving the parameter as object but never casting and validating it.
  • Assuming x:Static works when the enum is not publicly accessible.
  • Mixing enum types with identical member names and binding the wrong one.

Summary

  • Use x:Static in XAML to pass fixed enum values into commands.
  • Keep the enum defined in code and visible to the view.
  • Accept the parameter in the command handler as the enum type or cast safely.
  • Prefer enums over strings for clearer and safer MVVM command flow.
  • Debug namespace mapping and parameter casting first when the binding fails.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.