CommunityToolkit / CommunityToolkit/dotnet

RelayCommand<T> does not work with Enumerations

Open
#1,048 1 comment 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

If you try to bind an enum to a CommandParameter in UWP, then UWP tends to pass them around the system as the raw type of the enum as opposed to the .net type.

This means that when you bind to it and it fires the CanExecute(object?), that it fails because the type does not match (uint != SortOrder)

### Regression

_No response_

### Steps to reproduce

I've provided a bare-bones reproduction here:
https://github.com/[AtomicBlom/EnumRelayCommandTest](https://github.com/AtomicBlom/EnumRelayCommandTest)

But if you create a blank app, and add a ViewModel based on ObservableObject, something like this:

```csharp
public partial class MainPageViewModel : ObservableObject
{
[RelayCommand]
private void OnSetSortOrder(SortOrder order)
{
Debug.WriteLine($"Sort order set to {order}");
}
}

public enum SortOrder : uint
{
Ascending,
Descending
}
```

Then attempt to bind to it.
```xaml


Ascending


```

You'll find that it fails

### Expected behavior

You should be able to pass an enum as a CommandParameter

### Screenshots

_No response_

### IDE and version

VS 2022

### IDE version

17.12.4

### Nuget packages

- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)

### Nuget package version(s)

8.4.0

### Additional context

This error was also observed while attempting to port a UWP application to .net9

### Help us help you

Yes, I'd like to be assigned to work on this item

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.