Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RelayCommand<T> does not work with Enumerations #1048

Open
1 of 4 tasks
AtomicBlom opened this issue Feb 1, 2025 · 0 comments
Open
1 of 4 tasks

RelayCommand<T> does not work with Enumerations #1048

AtomicBlom opened this issue Feb 1, 2025 · 0 comments
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@AtomicBlom
Copy link

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:

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.

		<Button Command="{x:Bind ViewModel.SetSortOrderCommand, Mode=OneWay}" Content="Press me">
			<Button.CommandParameter>
				<local:SortOrder>Ascending</local:SortOrder>
			</Button.CommandParameter>
		</Button>

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
  • 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

@AtomicBlom AtomicBlom added the bug 🐛 An unexpected issue that highlights incorrect behavior label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

1 participant